トランザクションメールを送信し、受信メールを 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: "user@example.com",
from: "welcome@yourdomain.com",
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("support@yourdomain.com")) {
await message.forward("team@yourdomain.com");
}
// Send auto-reply
await env.EMAIL.send({
to: message.from,
from: "noreply@yourdomain.com",
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": "user@example.com",
"from": "welcome@yourdomain.com",
"subject": "Welcome to our service!",
"html": "<h1>Welcome!</h1><p>Thanks for signing up.</p>",
"text": "Welcome! Thanks for signing up."
}'cat > mail.txt <<EOF
From: welcome@yourdomain.com
To: user@example.com
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 "welcome@yourdomain.com" \
--mail-rcpt "user@example.com" \
--upload-file mail.txtREST API、Workers バインディング、SMTP の全体は API リファレンス を参照してください。
始める高い到達性とグローバルなパフォーマンスで、トランザクションメールを送信します。
受信メールをカスタムアドレス、Worker、または外部の宛先へルーティングします。
IP レピュテーションの自動管理と、到達性の最適化です。
包括的な指標とアラートで、メールのパフォーマンスを監視します。
REST API、Workers バインディング、または SMTP でメールを送信・ルーティングします。
エッジから直接メールを送れるサーバーレスアプリケーションを構築します。
Workers Queues 連携で、メールイベントを非同期に処理します。
Workers Analytics Engine で、カスタムのメール指標を保存して分析します。
プラットフォームの制限
Email Service の制限とクォータを確認します。
料金
Email Service の料金とプランを理解します。
例
実践的な例と実装パターンを探します。
Discord
他の開発者と Email Service について質問し、議論します。
プロダクトの発表と開発者向けの更新をフォローします。