Skip to content

非公式 / UnofficialCloudflare, Inc. 非提携。アカウント情報は入力しないでください。Not affiliated with Cloudflare, Inc. Do not enter Cloudflare credentials here.Official docsこのサイトについてプライバシー

メールルーティング

wrangler dev で受信メールをシミュレートし、メールルーティング Worker をローカルでテストします

最終更新 Markdown で表示Agent セットアップ

wrangler dev で受信メールをシミュレートし、デプロイ前にルーティングロジックを確認します。

前提条件

  1. Cloudflare アカウント ↗ に登録します。
  2. Node.js ↗ をインストールします。

Node.js のバージョンマネージャー

権限の問題を避け、Node.js のバージョンを切り替えられるよう、Volta ↗ や nvm ↗ などの Node バージョンマネージャーを使います。このガイドの後半で説明する Wrangler には、Node バージョン 16.17.0 以降が必要です。

設定

Wrangler ファイルを設定します。

{
	"name": "email-routing-worker",
	// Set this to today's date
	"compatibility_date": "2026-11-14",
}
name = "email-routing-worker"
# Set this to today's date
compatibility_date = "2026-11-14"

基本的なルーティング Worker

import * as PostalMime from "postal-mime";

export default {
	async email(message, env, ctx) {
		// Parse the raw email message
		const parser = new PostalMime.default();
		const rawEmail = new Response(message.raw);
		const email = await parser.parse(await rawEmail.arrayBuffer());

		console.log("Received email:", {
			from: message.from,
			to: message.to,
			subject: email.subject,
			text: email.text,
			html: email.html,
		});

		// Route based on recipient
		if (message.to.includes("support@")) {
			await message.forward("[email protected]");
		} else {
			await message.forward("[email protected]");
		}
	},
};

テスト

開発サーバーを起動します。

npx wrangler dev

ローカルエンドポイントでテストメールを送信します。リクエスト本文は RFC 5322 ↗ 形式の生のメールメッセージである必要があり、メッセージには Message-ID ヘッダーが必要です。

curl --request POST 'http://localhost:8787/cdn-cgi/local/email' \
  --url-query '[email protected]' \
  --url-query '[email protected]' \
  --data-raw 'Received: from smtp.example.com (127.0.0.1)
        by cloudflare-email.com (unknown) id 4fwwffRXOpyR
        for <[email protected]>; Tue, 27 Aug 2024 15:50:20 +0000
From: "John" <[email protected]>
Reply-To: [email protected]
To: [email protected]
Subject: Testing Email Workers Local Dev
Content-Type: text/html; charset="windows-1252"
X-Mailer: Curl
Date: Tue, 27 Aug 2024 08:49:44 -0700
Message-ID: <6114391943504294873000@ZSH-GHOSTTY>

Hi there'

コンソールに、解析されたメール構造が出力されます。

{
	"headers": [
		{
			"key": "received",
			"value": "from smtp.example.com (127.0.0.1) by cloudflare-email.com (unknown) id 4fwwffRXOpyR for <[email protected]>; Tue, 27 Aug 2024 15:50:20 +0000"
		},
		{ "key": "from", "value": "\"John\" <[email protected]>" },
		{ "key": "reply-to", "value": "[email protected]" },
		{ "key": "to", "value": "[email protected]" },
		{ "key": "subject", "value": "Testing Email Workers Local Dev" },
		{ "key": "content-type", "value": "text/html; charset=\"windows-1252\"" },
		{ "key": "x-mailer", "value": "Curl" },
		{ "key": "date", "value": "Tue, 27 Aug 2024 08:49:44 -0700" },
		{
			"key": "message-id",
			"value": "<6114391943504294873000@ZSH-GHOSTTY>"
		}
	],
	"from": { "address": "[email protected]", "name": "John" },
	"to": [{ "address": "[email protected]", "name": "" }],
	"replyTo": [{ "address": "[email protected]", "name": "" }],
	"subject": "Testing Email Workers Local Dev",
	"messageId": "<6114391943504294873000@ZSH-GHOSTTY>",
	"date": "2024-08-27T15:49:44.000Z",
	"html": "Hi there\n",
	"attachments": []
}

次のステップ

役に立ちましたか?