使用任意支持 SMTP 的语言或客户端,通过 Cloudflare Email Service 已认证 SMTP(smtp.mx.cloudflare.net:465)发送事务性邮件。
- 已为 Email Sending 接入域名。
- 具有 Email Sending: Edit 权限的 Cloudflare API 令牌。请在环境中将其设为
CF_API_TOKEN。该令牌用作 SMTP 密码;用户名为字面字符串api_token。
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:$CF_API_TOKEN" \
--mail-from "[email protected]" \
--mail-rcpt "[email protected]" \
--upload-file mail.txt发件人域名必须已在拥有该 API 令牌的账户上为 Email Sending 接入。
使用 npm install nodemailer 安装 Nodemailer ↗。
import nodemailer from "nodemailer";
const transporter = nodemailer.createTransport({
host: "smtp.mx.cloudflare.net",
port: 465,
secure: true, // implicit TLS
auth: {
user: "api_token",
pass: process.env.CF_API_TOKEN,
},
});
const info = await transporter.sendMail({
from: '"Acme" <[email protected]>',
to: "[email protected]",
subject: "Welcome to Acme",
text: "Thanks for signing up.",
html: "<h1>Welcome to Acme</h1><p>Thanks for signing up.</p>",
});
console.log("Message sent:", info.messageId);const info = await transporter.sendMail({
from: '"Acme Billing" <[email protected]>',
to: "[email protected]",
subject: "Your invoice",
text: "Please find your invoice attached.",
attachments: [
{
filename: "invoice-2026-04.pdf",
path: "./invoices/invoice-2026-04.pdf",
contentType: "application/pdf",
},
],
});消息总大小(含 base64 编码的附件)不得超过 5 MiB。请参阅限制。
Nodemailer 会以 Error 拒绝 promise,其 .responseCode 反映 SMTP 响应码。请参阅 SMTP 响应码 与故障排除。
try {
await transporter.sendMail({
/* ... */
});
} catch (err) {
console.error(err.responseCode, err.message);
}使用标准库 smtplib ↗(Python 3.8 或更高版本)。
import os
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg["From"] = "Acme <[email protected]>"
msg["To"] = "[email protected]"
msg["Subject"] = "Welcome to Acme"
msg.set_content("Thanks for signing up.")
msg.add_alternative(
"<h1>Welcome to Acme</h1><p>Thanks for signing up.</p>",
subtype="html",
)
with smtplib.SMTP_SSL("smtp.mx.cloudflare.net", 465) as s:
s.login("api_token", os.environ["CF_API_TOKEN"])
s.send_message(msg)smtplib.SMTP_SSL 会在端口 465 上打开隐式 TLS 连接,这是 Cloudflare SMTP 端点所要求的。请勿使用带 starttls() 的 smtplib.SMTP;不支持 STARTTLS。
msg["To"] = ", ".join([
"[email protected]",
"[email protected]",
"[email protected]",
])单个 SMTP 会话最多可投递 50 个 RCPT TO 地址。请参阅限制。
from pathlib import Path
pdf = Path("invoice-2026-04.pdf").read_bytes()
msg.add_attachment(
pdf,
maintype="application",
subtype="pdf",
filename="invoice-2026-04.pdf",
)smtplib 会抛出 smtplib.SMTPException 的子类,并附带 SMTP 响应码。请参阅 SMTP 响应码 与故障排除。
try:
with smtplib.SMTP_SSL("smtp.mx.cloudflare.net", 465) as s:
s.login("api_token", os.environ["CF_API_TOKEN"])
s.send_message(msg)
except smtplib.SMTPAuthenticationError as e:
print(f"Auth failed: {e.smtp_code} {e.smtp_error!r}")
except smtplib.SMTPResponseException as e:
print(f"SMTP error: {e.smtp_code} {e.smtp_error!r}")<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.mx.cloudflare.net';
$mail->Port = 465;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->SMTPAuth = true;
$mail->Username = 'api_token';
$mail->Password = getenv('CF_API_TOKEN');
$mail->setFrom('[email protected]', 'Acme');
$mail->addAddress('[email protected]');
$mail->Subject = 'Welcome to our service!';
$mail->Body = 'Thanks for signing up.';
$mail->send();