跳转到内容
搜索文档

更新日志

Cloudflare 的最新更新与改进。

Back to all posts

发送带有命名收件人地址的电子邮件

除了现有的 from 支持外,您现在还可以在发送电子邮件时为收件人地址添加显示名称。为 toccbccreplyTofrom 传递一个包含 email 和可选 name 字段的对象:

src/index.jsjs
export default {
	async fetch(request, env) {
		const response = await env.EMAIL.send({
			from: { email: "[email protected]", name: "Support Team" },
			to: { email: "[email protected]", name: "Jane Doe" },
			cc: [
				"[email protected]",
				{ email: "[email protected]", name: "Engineering Team" },
			],
			subject: "Welcome!",
			html: "<h1>Thanks for joining!</h1>",
			text: "Thanks for joining!",
		});

		return Response.json({ messageId: response.messageId });
	},
};
src/index.tsts
export default {
	async fetch(request, env): Promise<Response> {
		const response = await env.EMAIL.send({
			from: { email: "[email protected]", name: "Support Team" },
			to: { email: "[email protected]", name: "Jane Doe" },
			cc: [
				"[email protected]",
				{ email: "[email protected]", name: "Engineering Team" },
			],
			subject: "Welcome!",
			html: "<h1>Thanks for joining!</h1>",
			text: "Thanks for joining!",
		});

		return Response.json({ messageId: response.messageId });
	},
} satisfies ExportedHandler<Env>;

为实现向后兼容,纯字符串形式仍受到完整支持,并且您可以在同一个数组中混合使用字符串和命名对象。

有关完整的请求示例,请参阅 Workers APIREST API 文档。