---
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/vi/api-reference/webhooks/setup-events

Webhook

# Sự kiện giao email

Nhận thông báo theo thời gian thực về tình trạng giao email. Cấu hình URL webhook trong [dashboard](https://app.sendcorex.com/dashboard/webhooks) . Mọi sự kiện đều được ký bằng HMAC-SHA256 để bảo mật.

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

**Luôn xác minh chữ ký webhook.** Tạo secret webhook trong dashboard và dùng nó để xác minh yêu cầu đến. Tính HMAC trên body thô của yêu cầu, không phải JSON đã tuần tự hóa lại.

## Bắt đầu nhanh

Một listener xác minh chữ ký rồi xử lý sự kiện. Trả lời với trạng thái `2xx` khi sự kiện được chấp nhận.

```
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');
});
```

## Header

- `X-Webhook-Signature` string bắt buộc
Chữ ký HMAC-SHA256 của body yêu cầu. Định dạng: `sha256=hex_digest`

## Thử với ngrok

Đưa listener cục bộ ra internet và đăng ký URL được tạo làm URL webhook trong dashboard.

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

## Sự kiện

Mỗi sự kiện là một đối tượng JSON có `event`, `timestamp` và một đối tượng `data`. Mở một sự kiện để xem đầy đủ payload và trường.

### Giao thư

Điều gì xảy ra với email trên đường tới hộp thư.

[`queued` Gửi một lần khi email vào hàng đợi giao, với mọi email: API, SMTP và chiến dịch. Gửi lại khi email được chuyển sang IP hoặc máy chủ khác để thử lại.](https://docs.sendcorex.com/vi/api-reference/webhooks/events/queued) [`processing` Gửi khi email đã qua kiểm tra của chúng tôi và sắp được gửi tới máy chủ nhận.](https://docs.sendcorex.com/vi/api-reference/webhooks/events/processing) [`delivered` Gửi khi máy chủ thư nhận chấp nhận email.](https://docs.sendcorex.com/vi/api-reference/webhooks/events/delivered) [`bounced` Gửi khi email bị từ chối vĩnh viễn: địa chỉ không tồn tại, tên miền không nhận thư được, hoặc chúng tôi đã ngừng thử.](https://docs.sendcorex.com/vi/api-reference/webhooks/events/bounced) [`spam` Gửi khi máy chủ thư nhận từ chối email vì là spam hoặc lý do chính sách trong lúc giao.](https://docs.sendcorex.com/vi/api-reference/webhooks/events/spam) [`retrying` Gửi mỗi lần email chưa giao được và sẽ được thử lại.](https://docs.sendcorex.com/vi/api-reference/webhooks/events/retrying) [`failed` Gửi khi chúng tôi bỏ cuộc với email sau khi hết lượt thử lại.](https://docs.sendcorex.com/vi/api-reference/webhooks/events/failed) [`return_path` Gửi khi thư bounce (DSN) đến muộn, sau khi máy chủ nhận đã chấp nhận email. Lỗi chuyển email sang bounced. Thông báo trễ hoặc đã giao chỉ gửi sự kiện này.](https://docs.sendcorex.com/vi/api-reference/webhooks/events/return-path)

### Tracking

Người nhận làm gì sau khi email đến.

[`email_opened` Gửi khi người nhận mở email có bật theo dõi mở. Gồm thiết bị, IP, quốc gia và lượt mở có qua proxy hay không.](https://docs.sendcorex.com/vi/api-reference/webhooks/events/email-opened) [`email_clicked` Gửi khi người nhận nhấp link trong email có bật theo dõi nhấp. Gồm URL và đó có phải lần nhấp đầu hay không.](https://docs.sendcorex.com/vi/api-reference/webhooks/events/email-clicked) [`unsubscribe` Được gửi khi người nhận hủy đăng ký khỏi một email của bạn: trên trang hủy đăng ký, bằng nút một chạm của hộp thư (Gmail, Yahoo, Outlook) hoặc qua email nhờ header List-Unsubscribe.](https://docs.sendcorex.com/vi/api-reference/webhooks/events/unsubscribe) [`tracking_preferences_updated` Được gửi khi người nhận thay đổi lựa chọn theo dõi mở hoặc nhấp trên trang tùy chọn tracking. Họ vẫn tiếp tục nhận email của bạn: đây không phải là hủy đăng ký.](https://docs.sendcorex.com/vi/api-reference/webhooks/events/tracking-preferences-updated)

Endpoint lỗi quá thường xuyên? Trong dashboard bạn xem được trạng thái từng webhook, kích hoạt lại và thử lại các lần giao thất bại. [Mở Webhook trong dashboard](https://app.sendcorex.com/dashboard/webhooks)
