Skip to content

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

Cloudflare Email Service

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

トランザクションメールを送信し、受信メールを Worker またはメールアドレスへルーティングします

Cloudflare Email Service は、次のメール機能を提供します。

  • Email Sending ベータ — 送信トランザクションメール向け
    Workers Paid プランで利用できます
  • Email Routing — 受信メールを Worker で処理するか、メールアドレスへルーティングします
    Free および Paid プランで利用できます

この 2 つの機能を組み合わせると、アプリケーションからメールの送受信ができます。たとえば Email Service は次の用途に使えます。

  • トランザクションメール(ウェルカムメッセージ、パスワードリセット、注文確認)
  • 認証フロー(マジックリンク、メール検証、二要素認証)
  • 通知とアラート
  • カスタムメールアドレス(support@、contact@、orders@)
  • エージェントの操作手段としてのメール(チケット管理で issue を作成するメールなど)

Email Service には、Cloudflare Workers から バインディング で、任意のプラットフォームから REST API で、または 認証済み SMTP でアクセスできます。

src/index.ts で、EMAIL バインディングを使ってメールを送信し、email() ハンドラーで受信メールを処理します。

interface Env {
	EMAIL: SendEmail;
}

export default {
	// Handle HTTP requests (Email Sending)
	async fetch(request, env, ctx): Promise<Response> {
		// Send a welcome email
		await env.EMAIL.send({
			to: "[email protected]",
			from: "[email protected]",
			subject: "Welcome to our service!",
			html: "<h1>Welcome!</h1><p>Thanks for signing up.</p>",
			text: "Welcome! Thanks for signing up.",
		});

		return new Response("Email sent successfully");
	},

	// Handle incoming emails (Email Routing)
	async email(message, env, ctx): Promise<void> {
		// Forward to support team
		if (message.to.includes("[email protected]")) {
			await message.forward("[email protected]");
		}

		// Send auto-reply
		await env.EMAIL.send({
			to: message.from,
			from: "[email protected]",
			subject: "We received your message",
			html: "<h1>Thank you!</h1><p>We'll get back to you soon.</p>",
		});
	},
} satisfies ExportedHandler<Env>;

Wrangler 設定ファイルにバインディングを追加します。

{
	"$schema": "node_modules/wrangler/config-schema.json",
	"name": "<ENTER_WORKER_NAME>",
	"main": "src/index.ts",
	"compatibility_date": "$today",

	// Email sending
	"send_email": [
		{
			"name": "EMAIL"
		}
	],

	// Email routing
	"email": [
		{
			"name": "EMAIL_HANDLER"
		}
	]
}
curl "https://api.cloudflare.com/client/v4/accounts/{account_id}/email/sending/send" \
  --header "Authorization: Bearer <API_TOKEN>" \
  --header "Content-Type: application/json" \
  --data '{
    "to": "[email protected]",
    "from": "[email protected]",
    "subject": "Welcome to our service!",
    "html": "<h1>Welcome!</h1><p>Thanks for signing up.</p>",
    "text": "Welcome! Thanks for signing up."
  }'

Cloudflare は REST API 向けの公式 SDK も提供しています。Node、Python、Go です。

cat > mail.txt <<EOF
From: [email protected]
To: [email protected]
Subject: Welcome to our service!

Thanks for signing up.
EOF

curl --ssl-reqd \
  --url "smtps://smtp.mx.cloudflare.net:465" \
  --user "api_token:<API_TOKEN>" \
  --mail-from "[email protected]" \
  --mail-rcpt "[email protected]" \
  --upload-file mail.txt

REST API、Workers バインディング、SMTP の全体は API リファレンス を参照してください。

始める

機能

Email Sending

高い到達性とグローバルなパフォーマンスで、トランザクションメールを送信します。

Email Routing

受信メールをカスタムアドレス、Worker、または外部の宛先へルーティングします。

Deliverability

IP レピュテーションの自動管理と、到達性の最適化です。

Analytics & Observability

包括的な指標とアラートで、メールのパフォーマンスを監視します。

API

REST API、Workers バインディング、または SMTP でメールを送信・ルーティングします。


関連プロダクト

Workers

エッジから直接メールを送れるサーバーレスアプリケーションを構築します。

Queues

Workers Queues 連携で、メールイベントを非同期に処理します。

Analytics Engine

Workers Analytics Engine で、カスタムのメール指標を保存して分析します。


その他のリソース

料金

Email Service の料金とプランを理解します。

例

実践的な例と実装パターンを探します。

Discord

他の開発者と Email Service について質問し、議論します。

Twitter

プロダクトの発表と開発者向けの更新をフォローします。

役に立ちましたか?