---
agent:
  instructions: "Use this search API to find relevant content across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://docs.sendcorex.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://docs.sendcorex.com/llms.txt"
---

Source: https://docs.sendcorex.com/th/api-reference/webhooks/setup-events

Webhook

# เหตุการณ์การส่งอีเมล

รับการแจ้งเตือนแบบเรียลไทม์เกี่ยวกับสถานะการส่งอีเมล ตั้งค่า URL webhook ใน [แดชบอร์ด](https://app.sendcorex.com/dashboard/webhooks) ทุกเหตุการณ์ลงนามด้วย HMAC-SHA256 เพื่อความปลอดภัย

POST `https://your-server.com/webhook/email/delivery`

**ตรวจลายเซ็น webhook เสมอ** สร้าง secret ของ webhook ในแดชบอร์ดและใช้ตรวจคำขอที่เข้ามา คำนวณ HMAC จาก body ดิบของคำขอ ไม่ใช่ JSON ที่แปลงใหม่

## เริ่มต้นอย่างรวดเร็ว

listener ที่ตรวจลายเซ็นแล้วจัดการเหตุการณ์ ตอบด้วยสถานะ `2xx` เมื่อรับเหตุการณ์แล้ว

```
const express = require('express');
const crypto = require('crypto');

const app = express();
const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET;

function verifySignature(rawBody, signature) {
  if (!signature) return false;

  const expected = 'sha256=' + crypto
    .createHmac('sha256', WEBHOOK_SECRET)
    .update(rawBody)
    .digest('hex');

  const a = Buffer.from(signature);
  const b = Buffer.from(expected);
  // timingSafeEqual throws when the lengths differ
  return a.length === b.length && crypto.timingSafeEqual(a, b);
}

// express.raw keeps the body as a Buffer, exactly as it was sent
app.post('/webhook/email/delivery', express.raw({ type: 'application/json' }), (req, res) => {
  if (!verifySignature(req.body, req.headers['x-webhook-signature'])) {
    return res.status(401).json({ error: 'Invalid signature' });
  }

  const { event, emailId, data } = JSON.parse(req.body.toString('utf8'));

  console.log('[Email Delivery] ' + event + ' - ' + emailId);

  if (event === 'delivered') {
    console.log('   Recipient: ' + data.recipientEmail);
  } else if (event === 'bounced') {
    console.log('   Reason: ' + data.reason);
  }

  res.status(200).json({ received: true });
});

app.listen(3001, () => {
  console.log('Email delivery webhook listener on port 3001');
  console.log('   POST /webhook/email/delivery');
});
```

## เฮดเดอร์

- `X-Webhook-Signature` string จำเป็น
ลายเซ็น HMAC-SHA256 ของ body คำขอ รูปแบบ: `sha256=hex_digest`

## ทดสอบด้วย ngrok

เปิด listener ในเครื่องสู่อินเทอร์เน็ตและลงทะเบียน URL ที่ได้เป็น URL webhook ในแดชบอร์ด

```
ngrok http 3001
# Your webhook URL:
# https://abc123.ngrok.io/webhook/email/delivery
```

## เหตุการณ์

ทุกเหตุการณ์เป็นออบเจกต์ JSON ที่มี `event`, `timestamp` และออบเจกต์ `data` เปิดเหตุการณ์เพื่อดู payload และฟิลด์ทั้งหมด

### การส่ง

สิ่งที่เกิดกับอีเมลระหว่างทางไปกล่องจดหมาย

[`queued` ส่งครั้งเดียวเมื่ออีเมลเข้าคิวการส่ง สำหรับทุกอีเมล: API, SMTP และแคมเปญ ส่งอีกครั้งเมื่ออีเมลถูกย้ายไป IP หรือเซิร์ฟเวอร์อื่นเพื่อลองใหม่](https://docs.sendcorex.com/th/api-reference/webhooks/events/queued) [`processing` ส่งเมื่ออีเมลผ่านการตรวจของเราและกำลังจะส่งไปยังเซิร์ฟเวอร์ผู้รับ](https://docs.sendcorex.com/th/api-reference/webhooks/events/processing) [`delivered` ส่งเมื่อเซิร์ฟเวอร์อีเมลผู้รับยอมรับอีเมล](https://docs.sendcorex.com/th/api-reference/webhooks/events/delivered) [`bounced` ส่งเมื่ออีเมลถูกปฏิเสธถาวร: ไม่มีที่อยู่นี้ โดเมนรับอีเมลไม่ได้ หรือเราหยุดพยายามแล้ว](https://docs.sendcorex.com/th/api-reference/webhooks/events/bounced) [`spam` ส่งเมื่อเซิร์ฟเวอร์อีเมลผู้รับปฏิเสธอีเมลว่าเป็น spam หรือด้วยเหตุผลด้านนโยบายระหว่างการส่ง](https://docs.sendcorex.com/th/api-reference/webhooks/events/spam) [`retrying` ส่งทุกครั้งที่อีเมลยังส่งไม่ได้และจะลองใหม่](https://docs.sendcorex.com/th/api-reference/webhooks/events/retrying) [`failed` ส่งเมื่อเราเลิกส่งอีเมลหลังลองใหม่ครบแล้ว](https://docs.sendcorex.com/th/api-reference/webhooks/events/failed) [`return_path` ส่งเมื่อข้อความ bounce (DSN) มาถึงทีหลัง หลังจากเซิร์ฟเวอร์ผู้รับยอมรับอีเมลไปแล้ว ความล้มเหลวจะเปลี่ยนอีเมลเป็น bounced การแจ้งล่าช้าหรือส่งถึงจะส่งเฉพาะเหตุการณ์นี้](https://docs.sendcorex.com/th/api-reference/webhooks/events/return-path)

### Tracking

สิ่งที่ผู้รับทำหลังอีเมลมาถึง

[`email_opened` ส่งเมื่อผู้รับเปิดอีเมลที่เปิดการติดตามการเปิด รวมอุปกรณ์ IP ประเทศ และว่าการเปิดผ่านพร็อกซีหรือไม่](https://docs.sendcorex.com/th/api-reference/webhooks/events/email-opened) [`email_clicked` ส่งเมื่อผู้รับคลิกลิงก์ในอีเมลที่เปิดการติดตามการคลิก รวม URL และว่าเป็นการคลิกครั้งแรกหรือไม่](https://docs.sendcorex.com/th/api-reference/webhooks/events/email-clicked) [`unsubscribe` ส่งเมื่อผู้รับยกเลิกการรับจากอีเมลของคุณ: บนหน้ายกเลิกการรับ ด้วยปุ่มคลิกเดียวของกล่องจดหมาย (Gmail, Yahoo, Outlook) หรือทางอีเมลผ่านเฮดเดอร์ List-Unsubscribe](https://docs.sendcorex.com/th/api-reference/webhooks/events/unsubscribe) [`tracking_preferences_updated` ส่งเมื่อผู้รับเปลี่ยนตัวเลือกการติดตามการเปิดหรือการคลิกบนหน้าการตั้งค่า tracking ผู้รับยังคงได้รับอีเมลของคุณ นี่ไม่ใช่การยกเลิกการรับ](https://docs.sendcorex.com/th/api-reference/webhooks/events/tracking-preferences-updated)

endpoint ล้มเหลวบ่อยเกินไป? ในแดชบอร์ดคุณดูสถานะของแต่ละ webhook เปิดใช้อีกครั้ง และลองส่งที่ล้มเหลวซ้ำได้ [เปิด Webhook ในแดชบอร์ด](https://app.sendcorex.com/dashboard/webhooks)
