MailCraft
หน้าแรก ราคาเครดิต เอกสาร API
การเชื่อมต่อ

Webhooks และ Inbound Parse

รับแจ้งเหตุการณ์อีเมล (ส่งถึง ตีกลับ เปิดอ่าน คลิก ระงับ) และอีเมลขาเข้าเป็น HTTPS POST ที่ลงลายเซ็น พร้อมวิธีตรวจ header MailCraft-Signature

ภาพรวม

MailCraft จะส่ง HTTPS POST ไปยัง endpoint ของคุณทุกครั้งที่สถานะอีเมลเปลี่ยน สร้าง endpoint ได้ที่ แดชบอร์ด → Webhooks (เฉพาะเจ้าของบัญชี สมาชิกทีมดูได้อย่างเดียว) แต่ละบัญชีมีได้สูงสุด 10 endpoints เลือกรับเฉพาะ event ที่ต้องการ และจำกัดเฉพาะ Mail Agent เดียวได้

Event เกิดเมื่อ
email.delivered เซิร์ฟเวอร์ปลายทางรับอีเมลแล้ว (2xx)
email.deferred ส่งไม่สำเร็จชั่วคราว ระบบจะลองใหม่
email.bounced bounce_type: "hard" ถูกปฏิเสธถาวร (5xx หรือ DSN แบบ failed) · bounce_type: "soft" ลองครบแล้ว/หมดอายุ หรือได้ DSN แบบ delayed
email.opened มีการโหลด tracking pixel (ถ้าเปิดติดตามการเปิดอ่าน)
email.clicked มีการคลิกลิงก์ที่ติดตาม (ถ้าเปิดติดตามการคลิก)
email.suppressed มีที่อยู่ถูกเพิ่มในรายการระงับ (hard bounce, ร้องเรียน, ยกเลิกรับข่าวสาร, เพิ่มเอง)
inbound.received มีอีเมลเข้ามาที่ที่อยู่รับอีเมลของคุณ (ดู Inbound parse)

รูปแบบข้อมูล (Payload)

body ของทุก request เป็น JSON envelope เดียว:

{
  "id": "evt_6f1c2a54-3c1e-4a8e-9d55-0f6c3c7b2e11",
  "type": "email.bounced",
  "created_at": "2026-12-05T08:15:02Z",
  "account_id": "0b7d6c1e-5a43-4f7e-8d0a-2f3c1b9e7a10",
  "data": {
    "message_id": "9a3e…",
    "message_id_header": "<20261205081500.9a3e@mail.example.com>",
    "agent_id": "c1d2…",
    "recipient": "someone@example.net",
    "subject": "รหัส OTP ของคุณ",
    "status": "bounced",
    "smtp_code": 550,
    "smtp_response": "550 5.1.1 The email account that you tried to reach does not exist",
    "bounce_type": "hard"
  }
}

ฟิลด์ใน data ของ event email.*: message_id, message_id_header, agent_id, recipient, subject, status, smtp_code, smtp_response, bounce_type (bounced), url (clicked), ip และ user_agent (opened / clicked) ฟิลด์ที่ไม่เกี่ยวข้องจะไม่ถูกส่งมา ส่วน email.suppressed มี recipient, agent_id, status: "suppressed", reason และ source event ทดสอบจากแดชบอร์ดจะมี "test": true

การส่งเป็นแบบ at least once และไม่รับประกันลำดับ ใช้ id (ส่งมาใน header MailCraft-Event-Id ด้วย) กันการประมวลผลซ้ำ และใช้ created_at เรียงลำดับ

Headers

Header ค่า
Content-Type application/json
User-Agent MailCraft-Webhooks/1.0
MailCraft-Event-Id evt_<uuid> (เท่ากับ id)
MailCraft-Event-Type เช่น email.delivered
MailCraft-Signature t=<unix seconds>,v1=<hex signature>

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

แต่ละ endpoint มี signing secret รูปแบบ whsec_… ซึ่งแสดงครั้งเดียวตอนสร้าง (กด สร้าง secret ใหม่ เพื่อเปลี่ยน secret เดิมจะใช้ไม่ได้ทันที)

  1. อ่าน raw body ตามที่ได้รับจริง (ห้าม parse แล้ว serialize JSON ใหม่)
  2. แยกค่า t และ v1 จาก MailCraft-Signature
  3. คำนวณ HMAC-SHA256(key = secret ทั้งสตริงรวม "whsec_", message = t + "." + raw_body) แล้วแปลงเป็น hex ตัวพิมพ์เล็ก
  4. เปรียบเทียบกับ v1 แบบ constant-time
  5. ปฏิเสธ request ถ้า |เวลาปัจจุบัน - t| > 300 วินาที (กัน replay)

ตอบกลับด้วย 2xx ภายใน 10 วินาที ถ้าไม่ใช่ 2xx หรือหมดเวลา ระบบจะลองใหม่หลัง 1 นาที, 5 นาที, 30 นาที, 2 ชม., 6 ชม. และ 12 ชม. จากนั้นจึงถือว่าล้มเหลว ระบบไม่ตาม redirect ถ้า endpoint ล้มเหลวต่อเนื่อง 3 วัน จะถูกปิดอัตโนมัติและแจ้งเจ้าของบัญชีทางอีเมล ส่งซ้ำรายการใดก็ได้จากประวัติด้วยปุ่ม ส่งซ้ำ

Node.js (Express)

import crypto from 'node:crypto';
import express from 'express';

const app = express();
const SECRET = process.env.MAILCRAFT_WEBHOOK_SECRET; // whsec_...

app.post('/webhooks/mailcraft', express.raw({ type: 'application/json' }), (req, res) => {
  const header = req.get('MailCraft-Signature') || '';
  const parts = Object.fromEntries(header.split(',').map((p) => p.split('=', 2)));
  const t = Number(parts.t);
  if (!t || Math.abs(Date.now() / 1000 - t) > 300) return res.sendStatus(400);

  const expected = crypto.createHmac('sha256', SECRET).update(`${t}.`).update(req.body).digest('hex');
  const given = Buffer.from(parts.v1 || '', 'utf8');
  if (given.length !== expected.length || !crypto.timingSafeEqual(given, Buffer.from(expected, 'utf8'))) {
    return res.sendStatus(400);
  }

  const event = JSON.parse(req.body.toString('utf8'));
  // กันซ้ำด้วย event.id แล้วจัดการตาม event.type ...
  res.sendStatus(204);
});

PHP

<?php
$secret  = getenv('MAILCRAFT_WEBHOOK_SECRET'); // whsec_...
$payload = file_get_contents('php://input');
$header  = $_SERVER['HTTP_MAILCRAFT_SIGNATURE'] ?? '';

$parts = [];
foreach (explode(',', $header) as $kv) {
    [$k, $v] = array_pad(explode('=', $kv, 2), 2, '');
    $parts[$k] = $v;
}
$t = (int)($parts['t'] ?? 0);
if ($t === 0 || abs(time() - $t) > 300) { http_response_code(400); exit; }

$expected = hash_hmac('sha256', $t . '.' . $payload, $secret);
if (!hash_equals($expected, $parts['v1'] ?? '')) { http_response_code(400); exit; }

$event = json_decode($payload, true);
// กันซ้ำด้วย $event['id'] แล้วจัดการตาม $event['type'] ...
http_response_code(204);

Python (Flask)

import hmac, hashlib, json, os, time
from flask import Flask, request, abort

app = Flask(__name__)
SECRET = os.environ["MAILCRAFT_WEBHOOK_SECRET"].encode()  # whsec_...

@app.post("/webhooks/mailcraft")
def mailcraft_webhook():
    raw = request.get_data()  # raw bytes ก่อน parse JSON
    parts = dict(p.split("=", 1) for p in request.headers.get("MailCraft-Signature", "").split(",") if "=" in p)
    t = int(parts.get("t", "0"))
    if not t or abs(time.time() - t) > 300:
        abort(400)
    expected = hmac.new(SECRET, f"{t}.".encode() + raw, hashlib.sha256).hexdigest()
    if not hmac.compare_digest(expected, parts.get("v1", "")):
        abort(400)
    event = json.loads(raw)
    # กันซ้ำด้วย event["id"] แล้วจัดการตาม event["type"] ...
    return "", 204

Inbound parse

รับอีเมลขาเข้าแล้วส่งต่อเป็น event inbound.received ไม่มีค่าใช้จ่าย (ไม่ใช้เครดิต) ไปที่ Webhooks → อีเมลขาเข้า แล้วเพิ่มที่อยู่รับอีเมลที่ชี้ไปยัง endpoint ของคุณ:

  • ที่อยู่ฟรีของแพลตฟอร์ม: <ชื่อ>@inbound.<โดเมนแพลตฟอร์ม> ใช้ได้ทันที รองรับ +tag เช่น support+ticket-42@… จะเข้า route support
  • โดเมนของคุณเอง: เพิ่ม MX record ที่โดเมน (แนะนำให้ใช้ subdomain เฉพาะ เช่น inbound.example.com) แล้วกด ตรวจ MX
inbound.example.com.   MX   10   mx-bounce.<โดเมนแพลตฟอร์ม>.

เลือกรับทุกที่อยู่ในโดเมน (catch-all) หรือเฉพาะบางชื่อได้ ผู้รับที่ไม่รู้จักจะถูกปฏิเสธระหว่างการสนทนา SMTP ด้วยรหัส 550 ขนาดอีเมลสูงสุด 10 MB

ตัวอย่าง data ของ inbound.received:

{
  "inbound_id": "5c0f…",
  "route_id": "a71e…",
  "from": { "name": "สมชาย", "address": "somchai@example.net" },
  "to": [{ "name": "Support", "address": "support@inbound.example.com" }],
  "cc": [],
  "subject": "คำสั่งซื้อ #42",
  "text": "สวัสดีครับ…",
  "html": "<p>สวัสดีครับ…</p>",
  "headers": { "message-id": "<…>", "date": "…", "in-reply-to": "<…>" },
  "attachments": [
    { "filename": "invoice.pdf", "content_type": "application/pdf", "size": 48213, "content_base64": "JVBERi0…" }
  ],
  "envelope": { "from": "somchai@example.net", "to": ["support@inbound.example.com"] },
  "remote_ip": "203.0.113.25",
  "helo": "mail.example.net",
  "dkim": [{ "domain": "example.net", "result": "pass" }],
  "spf": "not_checked",
  "raw_size": 51022
}

เนื้อหาไฟล์แนบจะส่งเป็น base64 เมื่อไฟล์แนบทั้งหมดรวมกันไม่เกิน 5 MB ถ้าเกิน จะไม่ส่ง content_base64 แต่ละไฟล์จะมี "truncated": true และ attachments_truncated เป็น true ระบบเก็บอีเมลต้นฉบับไว้ 7 วัน