Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

メールを送信する

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

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

Cloudflare Email Service を使い、アプリケーションからメールを送信します。Cloudflare Workers 上のアプリケーションには Workers バインディング、任意のプラットフォームからは REST API、SMTP 対応のアプリケーションやメールクライアントからは SMTP を使えます。

ドメインを設定する

Email Sending を使う前に、ドメインを設定します。

  1. Cloudflare ダッシュボードで、Compute > Email Service > Email Sending に移動します。

    Email Sending を開く ↗
  2. Onboard Domain を選択します。

  3. Cloudflare アカウントのドメインを選びます。任意で、Cloudflare がドメインの cf-bounce サブドメインに追加する DNS レコードを確認します。

    • バウンスメールを Cloudflare へルーティングする MX レコード。
    • メール送信を認可する SPF 用の TXT レコード。
    • ドメインから送信するメールを認証する DKIM 用の TXT レコード。
    • _dmarc.yourdomain.com の DMARC 用 TXT レコード。
  4. Done を選択します。

ドメインのオンボードが完了したら、メールの送信を開始できます。

最初のメールを送信する

Workers バインディング、REST API、または SMTP で最初のメールを送信できます。

Cloudflare Workers 上で構築している場合は、ネイティブなメール送信に Workers バインディングを使えます。まず新しい Worker プロジェクトを作成します。

  1. 新しい Worker プロジェクトを作成します。

    npm create cloudflare@latest -- email-service-tutorial

    プロンプトが表示されたら、テンプレートとして "Hello World" Worker を選択します。

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

    {
    	"send_email": [
    		{
    			"name": "EMAIL",
    			"remote": true,
    		},
    	],
    }
    [[send_email]]
    name = "EMAIL"
    remote = true
  3. src/index.ts に Worker のコードを作成します。

    // Configuration - Update these values
    const YOUR_DOMAIN = "yourdomain.com"; // Replace with your verified domain
    const RECIPIENT_EMAIL = "recipient@example.com"; // Replace with your email to receive test emails
    
    export default {
    	async fetch(request: Request, env: Env): Promise<Response> {
    		// Send a welcome email
    		const response = await env.EMAIL.send({
    			to: RECIPIENT_EMAIL,
    			from: `welcome@${YOUR_DOMAIN}`,
    			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: ${response.messageId}`);
    	},
    } satisfies ExportedHandler<Env>;
  4. npx wrangler dev で Worker プロジェクトを開発し、メールを送信します。コードはローカルで動き、Cloudflare Email Service に接続します(リモートバインディング を使用)。

    npx wrangler dev
    # ⎔ Starting remote preview...
    # Total Upload: 24.96 KiB / gzip: 6.17 KiB
    # [wrangler:info] Ready on http://localhost:8787
  5. Worker をデプロイします。

    npm run deploy

デプロイ後、Worker がメールを送信できることを確認します。

  1. ブラウザーで Worker の URL を開きます(デプロイ出力に表示されます。例: https://email-service-tutorial.<your-subdomain>.workers.dev)。
  2. Email sent: <message-id> のようなレスポンスが表示されます。
  3. RECIPIENT_EMAIL に指定したメールアドレスの受信箱を確認します。メールが見当たらない場合は、迷惑メールフォルダーも確認します。

curl コマンド 1 つでメールを送信します。{account_id}Cloudflare アカウント ID に、<API_TOKEN>API トークン に置き換えます。

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": "recipient@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."
  }'

成功時のレスポンスには、各受信者の配信状態が含まれます。

{
	"success": true,
	"errors": [],
	"messages": [],
	"result": {
		"delivered": ["recipient@example.com"],
		"permanent_bounces": [],
		"queued": []
	}
}

詳細は REST API リファレンス を参照してください。

curl コマンド 1 つでメールを送信します。<API_TOKEN>Email Sending: Edit 権限のある Cloudflare API トークン に置き換え、--mail-from--mail-rcpt のアドレスも自分のものに置き換えます。

cat > mail.txt <<EOF
From: welcome@yourdomain.com
To: recipient@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 "recipient@example.com" \
  --upload-file mail.txt

送信元ドメイン(welcome@yourdomain.com)は、API トークンを所有するアカウントで Email Sending にオンボードされている必要があります。

接続の詳細、認証、応答コード、言語別の例は、SMTP リファレンス を参照してください。

次のステップ

メールを送信できるようになったら、高度な機能を確認します。

役に立ちましたか?