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

Webhook

# メール配信イベント

メールの配信状況をリアルタイムで通知します。Webhook URLは [ダッシュボード](https://app.sendcorex.com/dashboard/webhooks) で設定します。すべてのイベントはセキュリティのためHMAC-SHA256で署名されています。

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

**Webhookの署名は必ず検証してください。** ダッシュボードでWebhookのシークレットを生成し、受信したリクエストの検証に使います。HMACは再シリアライズしたJSONではなく、生のリクエストボディで計算してください。

## クイックスタート

署名を検証してからイベントを処理するリスナーです。イベントを受け付けたら `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署名。形式： `sha256=hex_digest`

## ngrokでテスト

ローカルのリスナーをインターネットに公開し、生成されたURLをダッシュボードでWebhook URLとして登録します。

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

## イベント

すべてのイベントは `event`, `timestamp` と `data`オブジェクトを持つJSONです。イベントを開くと、完全なペイロードと項目を確認できます。

### 配信

受信トレイに届くまでにメールに起きること。

[`queued` APIやSMTP、キャンペーンなど、すべてのメールが配信キューに入ったときに1回送られます。再試行のために別のIPやサーバーへ移されたときにも再度送られます。](https://docs.sendcorex.com/ja/api-reference/webhooks/events/queued) [`processing` メールが当社のチェックを通過し、受信側サーバーに送られる直前に送られます。](https://docs.sendcorex.com/ja/api-reference/webhooks/events/processing) [`delivered` 受信側メールサーバーがメールを受け付けたときに送られます。](https://docs.sendcorex.com/ja/api-reference/webhooks/events/delivered) [`bounced` メールが最終的に拒否されたときに送られます：アドレスが存在しない、ドメインがメールを受信できない、または再試行を停止した場合。](https://docs.sendcorex.com/ja/api-reference/webhooks/events/bounced) [`spam` 配信中に受信側メールサーバーがspamまたはポリシー上の理由でメールを拒否したときに送られます。](https://docs.sendcorex.com/ja/api-reference/webhooks/events/spam) [`retrying` メールをまだ配信できず、再試行する予定のときに毎回送られます。](https://docs.sendcorex.com/ja/api-reference/webhooks/events/retrying) [`failed` 再試行を使い切ってメールの配信をあきらめたときに送られます。](https://docs.sendcorex.com/ja/api-reference/webhooks/events/failed) [`return_path` 受信側サーバーが一度メールを受け付けた後で、bounceメッセージ（DSN）が届いたときに送られます。失敗の場合、メールはbouncedになります。遅延や配信の通知ではこのイベントのみ送られます。](https://docs.sendcorex.com/ja/api-reference/webhooks/events/return-path)

### トラッキング

届いた後に受信者が行うこと。

[`email_opened` 開封トラッキングがオンのメールを受信者が開封したときに送られます。端末、IP、国、プロキシ経由かどうかを含みます。](https://docs.sendcorex.com/ja/api-reference/webhooks/events/email-opened) [`email_clicked` クリックトラッキングがオンのメールで受信者がリンクをクリックしたときに送られます。URLと初回クリックかどうかを含みます。](https://docs.sendcorex.com/ja/api-reference/webhooks/events/email-clicked) [`unsubscribe` 受信者があなたのメールから配信停止したときに送られます：配信停止ページ、メールボックスのワンクリックボタン（Gmail、Yahoo、Outlook）、またはList-Unsubscribeヘッダーによるメール。](https://docs.sendcorex.com/ja/api-reference/webhooks/events/unsubscribe) [`tracking_preferences_updated` 受信者がトラッキング設定ページで開封またはクリックのトラッキングの選択を変更したときに送られます。メールは引き続き届きます。配信停止ではありません。](https://docs.sendcorex.com/ja/api-reference/webhooks/events/tracking-preferences-updated)

エンドポイントの失敗が多すぎますか？ダッシュボードで各Webhookの状態を確認し、再有効化や失敗した配信の再試行ができます。 [ダッシュボードでWebhookを開く](https://app.sendcorex.com/dashboard/webhooks)
