MailCraft
หน้าแรก ราคาเครดิต เอกสาร API
SDK และปลั๊กอิน

SDK สำหรับ Python

ติดตั้งแพ็กเกจ mailcraft แล้วส่งอีเมลจาก Python 3.9+ ด้วยไลบรารีมาตรฐานล้วน ไม่มี dependency เพิ่ม

SDK ทางการสำหรับ Python 3.9 ขึ้นไป ใช้แค่ไลบรารีมาตรฐาน (urllib) จึงติดตั้งได้ทุกที่ ทั้ง Django, Flask, FastAPI หรือสคริปต์ cron ธรรมดา มี type hints ครบ

ติดตั้ง

pip install mailcraft

ส่งอีเมลฉบับแรก

from mailcraft import MailCraft

client = MailCraft()  # อ่านคีย์จาก MAILCRAFT_API_KEY

res = client.send(
    {
        "from": "ร้านตัวอย่าง <no-reply@mail.example.com>",
        "to": "customer@example.org",
        "subject": "ยืนยันคำสั่งซื้อ #10042",
        "htmlbody": "<h1>ขอบคุณที่สั่งซื้อ</h1><p>เรากำลังเตรียมจัดส่งสินค้าให้คุณ</p>",
        "textbody": "ขอบคุณที่สั่งซื้อ เรากำลังเตรียมจัดส่งสินค้าให้คุณ",
    },
    idempotency_key="order-10042-receipt",
)

print(res["message_id"], res["credits_remaining"])

จะเขียนเป็น keyword arguments ก็ได้ แต่ต้องใช้ from_ แทน from เพราะ from เป็นคำสงวนของ Python

client.send(
    from_="no-reply@mail.example.com",
    to=["a@example.org", {"address": "b@example.org", "name": "บี"}],
    subject="แจ้งปิดปรับปรุงระบบ",
    textbody="ระบบจะปิดปรับปรุงคืนนี้ 23:00–01:00 น.",
)

แนบไฟล์

from mailcraft import Attachment

with open("INV-001.pdf", "rb") as f:
    pdf = f.read()

client.send({
    "from": "billing@mail.example.com",
    "to": "customer@example.org",
    "subject": "ใบเสร็จรับเงิน INV-001",
    "textbody": "แนบใบเสร็จมากับอีเมลนี้",
    "attachments": [Attachment("INV-001.pdf", pdf, "application/pdf")],
})

เทมเพลต ตั้งเวลาส่ง ส่งเป็นชุด และยกเลิก

from datetime import datetime, timedelta, timezone

BANGKOK = timezone(timedelta(hours=7))

r = client.send(
    from_="hello@mail.example.com",
    to="somchai@example.org",
    template_alias="welcome",
    template_data={"name": "สมชาย"},
    send_at=datetime(2027, 1, 2, 9, 0, tzinfo=BANGKOK),
    list_unsubscribe=True,
)

client.cancel(r["message_id"])
client.send_batch([message1, message2])
status = client.get(r["message_id"])

datetime ที่ไม่ระบุ timezone จะถือว่าเป็น UTC แนะนำให้ระบุ timezone ทุกครั้ง

จัดการข้อผิดพลาด

from mailcraft import MailCraftError, InsufficientCreditsError, RateLimitError

try:
    client.send(message)
except InsufficientCreditsError as e:
    print(f"เครดิตเหลือ {e.balance} แต่ต้องใช้ {e.required}")
except RateLimitError as e:
    print(f"ส่งถี่เกินไป ลองใหม่ใน {e.retry_after} วินาที")
except MailCraftError as e:
    print(e.status, e.code, e.field, e.message)

คลาสย่อยที่มีให้: ValidationError, AuthenticationError, InsufficientCreditsError, PermissionDeniedError, NotFoundError, ConflictError, PayloadTooLargeError, RateLimitError, ServerError และ APIConnectionError (network_error หรือ timeout)

ลองส่งซ้ำอัตโนมัติ

ลองใหม่สูงสุด 2 ครั้งเมื่อเจอ 429 / 5xx / เครือข่ายขัดข้อง รอตาม Retry-After และแนบ Idempotency-Key ให้เองทุกครั้งที่ส่ง จึงไม่ส่งซ้ำและไม่หักเครดิตซ้ำ

client = MailCraft(api_key="maas_live_...", timeout=30, max_retries=2, max_retry_delay=60)

ตรวจลายเซ็น Webhook

from mailcraft import parse_event

# Flask: request.get_data()  ·  Django: request.body
event = parse_event(raw_body, request.headers.get("MailCraft-Signature"), secret)

ดูตัวอย่าง Flask และ Django ได้ที่ ตรวจสอบลายเซ็น Webhook