使用 Amazon SES 发送电子邮件 - AWS SDK for JavaScript

AWS SDK for JavaScript V3 API 参考指南详细描述了 AWS SDK for JavaScript 版本 3 (V3) 的所有 API 操作。

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Amazon SES 发送电子邮件

JavaScript code example that applies to Node.js execution

此 Node.js 代码示例演示:

  • 发送文本或 HTML 电子邮件。

  • 根据电子邮件模板发送电子邮件。

  • 根据电子邮件模板批量发送电子邮件。

Amazon SES API 为您提供了两种不同的方法来发送电子邮件,具体取决于您对电子邮件内容的控制程度:格式化和原始。有关详细信息,请参阅使用 Amazon SES API 发送格式化电子邮件使用 Amazon SES API 发送原始电子邮件

情景

在本示例中,您使用一系列 Node.js 模块以多种方式发送电子邮件。Node.js 模块使用的 SDK JavaScript ,使用SES客户端类的以下方法来创建和使用电子邮件模板:

完成先决条件任务

要设置和运行此示例,您必须先完成以下任务:

  • 设置项目环境以运行这些 Node TypeScript 示例,并安装所需的模块 AWS SDK for JavaScript 和第三方模块。按照上的说明进行操作 GitHub

  • 使用用户凭证创建共享配置文件。有关提供共享凭证文件的更多信息,请参阅《AWS SDK 和工具参考指南》 中的共享配置和凭证文件

重要

这些示例演示了如何使用 ECMAScript6(ES6)导入/导出客户端服务对象和命令。

电子邮件发送要求

Amazon SES 编写电子邮件并立即将其加入队列等待发送。要使用 SendEmailCommand 方法发送电子邮件,您的邮件必须满足以下要求:

  • 您必须从已验证的电子邮件地址或域发送邮件。如果您尝试使用未验证的地址或域发送电子邮件,则操作会导致 "Email address not verified" 错误。

  • 如果您的账户仍在 Amazon SES 沙盒中,则只能发送到经验证的地址或域,或者与 Amazon SES 邮箱模拟器关联的电子邮件地址。有关更多信息,请参阅《Amazon Simple Email Service 开发人员指南》中的验证电子邮件地址和域

  • 邮件(包括附件)的总大小必须小于 10 MB。

  • 邮件必须包含至少一个收件人电子邮件地址。收件人地址可以是“收件人:”地址、“抄送:”地址或“密件抄送:”地址。如果某个收件人的电子邮件地址无效(即,未使用格式 UserName@[SubDomain.]Domain.TopLevelDomain),则将拒绝整个邮件,即使邮件包含的其他收件人有效。

  • 邮件在“收件人:”、“抄送:”和“密件抄送:”字段中包含的收件人不能超过 50 个。如果您需要将电子邮件发送给更多的受众,可以将收件人列表划分为不超过 50 个人的组,然后多次调用 sendEmail 方法来发送邮件到各个组。

发送电子邮件

在本示例中,使用 Node.js 模块通过 Amazon SES 发送电子邮件。

创建一个 libs 目录,然后使用文件名 sesClient.js 创建一个 Node.js 模块。将以下代码复制并粘贴到其中,这将创建 Amazon SES 客户端对象。将 “区域” 替换为您的 “ AWS 区域”。

import { SESClient } from "@aws-sdk/client-ses"; // Set the AWS Region. const REGION = "us-east-1"; // Create SES service object. const sesClient = new SESClient({ region: REGION }); export { sesClient };

可以在此处找到此示例代码 GitHub。

创建文件名为 ses_sendemail.js 的 Node.js 模块。如前所示配置 SDK,包括安装所需的客户端和软件包。

创建一个对象以将定义要发送的电子邮件的参数值传递到 SES 客户端类的 SendEmailCommand 方法,这些参数值包括发件人和收件人地址、主题、纯文本和 HTML 格式的电子邮件正文。要调用 SendEmailCommand 方法,请调用一个 Amazon SES 服务对象来传递参数。

注意

此示例导入并使用所需的 S AWS ervice V3 包客户端、V3 命令,并以异步/等待模式使用该send方法。您可以改用 V2 命令创建此示例,方法是进行一些细微的更改。有关更多信息,请参阅 使用 V3 命令

注意

RECEIVER_ADDRESS 替换为要将电子邮件发送到的地址,将 SENDER_ADDRESS 替换为发送电子邮件的电子邮件地址。

import { SendEmailCommand } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; const createSendEmailCommand = (toAddress, fromAddress) => { return new SendEmailCommand({ Destination: { /* required */ CcAddresses: [ /* more items */ ], ToAddresses: [ toAddress, /* more To-email addresses */ ], }, Message: { /* required */ Body: { /* required */ Html: { Charset: "UTF-8", Data: "HTML_FORMAT_BODY", }, Text: { Charset: "UTF-8", Data: "TEXT_FORMAT_BODY", }, }, Subject: { Charset: "UTF-8", Data: "EMAIL_SUBJECT", }, }, Source: fromAddress, ReplyToAddresses: [ /* more items */ ], }); }; const run = async () => { const sendEmailCommand = createSendEmailCommand( "recipient@example.com", "sender@example.com", ); try { return await sesClient.send(sendEmailCommand); } catch (e) { console.error("Failed to send email."); return e; } };

要运行示例,请在命令提示符中键入以下内容。电子邮件会排队,等待由 Amazon SES 发送。

node ses_sendemail.js

可以在此处找到此示例代码 GitHub。

使用模板发送电子邮件

在本示例中,使用 Node.js 模块通过 Amazon SES 发送电子邮件。创建文件名为 ses_sendtemplatedemail.js 的 Node.js 模块。如前所示配置 SDK,包括安装所需的客户端和软件包。

创建一个对象以将定义要发送的电子邮件的参数值传递到 SES 客户端类的 SendTemplatedEmailCommand 方法,这些参数值包括发件人和收件人地址、主题、纯文本和 HTML 格式的电子邮件正文。要调用 SendTemplatedEmailCommand 方法,请调用一个 Amazon SES 客户端服务对象来传递参数。

注意

此示例导入并使用所需的 S AWS ervice V3 包客户端、V3 命令,并以异步/等待模式使用该send方法。您可以改用 V2 命令创建此示例,方法是进行一些细微的更改。有关更多信息,请参阅 使用 V3 命令

注意

将 REGION 替换为您所在的地 AWS 区,将 RECEIVER_ ADDRESS 替换为要发送电子邮件的地址,将 SENDER_ ADDRESS 替换为发送电子邮件的电子邮件地址,将 TEMPLATE_NAME 替换为模板名称

import { SendTemplatedEmailCommand } from "@aws-sdk/client-ses"; import { getUniqueName, postfix, } from "@aws-doc-sdk-examples/lib/utils/util-string.js"; import { sesClient } from "./libs/sesClient.js"; /** * Replace this with the name of an existing template. */ const TEMPLATE_NAME = getUniqueName("ReminderTemplate"); /** * Replace these with existing verified emails. */ const VERIFIED_EMAIL = postfix(getUniqueName("Bilbo"), "@example.com"); const USER = { firstName: "Bilbo", emailAddress: VERIFIED_EMAIL }; /** * * @param { { emailAddress: string, firstName: string } } user * @param { string } templateName - The name of an existing template in Amazon SES. * @returns { SendTemplatedEmailCommand } */ const createReminderEmailCommand = (user, templateName) => { return new SendTemplatedEmailCommand({ /** * Here's an example of how a template would be replaced with user data: * Template: <h1>Hello {{contact.firstName}},</h1><p>Don't forget about the party gifts!</p> * Destination: <h1>Hello Bilbo,</h1><p>Don't forget about the party gifts!</p> */ Destination: { ToAddresses: [user.emailAddress] }, TemplateData: JSON.stringify({ contact: { firstName: user.firstName } }), Source: VERIFIED_EMAIL, Template: templateName, }); }; const run = async () => { const sendReminderEmailCommand = createReminderEmailCommand( USER, TEMPLATE_NAME, ); try { return await sesClient.send(sendReminderEmailCommand); } catch (err) { console.log("Failed to send template email", err); return err; } };

要运行示例,请在命令提示符中键入以下内容。电子邮件会排队,等待由 Amazon SES 发送。

node ses_sendtemplatedemail.js

可以在此处找到此示例代码 GitHub。

使用模板批量发送电子邮件

在本示例中,使用 Node.js 模块通过 Amazon SES 发送电子邮件。

创建一个 libs 目录,然后使用文件名 sesClient.js 创建一个 Node.js 模块。将以下代码复制并粘贴到其中,这将创建 Amazon SES 客户端对象。将 “区域” 替换为您的 “ AWS 区域”。

import { SESClient } from "@aws-sdk/client-ses"; // Set the AWS Region. const REGION = "us-east-1"; // Create SES service object. const sesClient = new SESClient({ region: REGION }); export { sesClient };

可以在此处找到此示例代码 GitHub。

创建文件名为 ses_sendbulktemplatedemail.js 的 Node.js 模块。如前所示配置 SDK,包括安装所需的客户端和软件包。

创建一个对象以将定义要发送的电子邮件的参数值传递到 SES 客户端类的 SendBulkTemplatedEmailCommand 方法,这些参数值包括发件人和收件人地址、主题、纯文本和 HTML 格式的电子邮件正文。要调用 SendBulkTemplatedEmailCommand 方法,请调用一个 Amazon SES 服务对象来传递参数。

注意

此示例导入并使用所需的 S AWS ervice V3 包客户端、V3 命令,并以异步/等待模式使用该send方法。您可以改用 V2 命令创建此示例,方法是进行一些细微的更改。有关更多信息,请参阅 使用 V3 命令

注意

RECEIVER_ADDRESSES 替换为要将电子邮件发送到的地址,将 SENDER_ADDRESS 替换为发送电子邮件的电子邮件地址。

import { SendBulkTemplatedEmailCommand } from "@aws-sdk/client-ses"; import { getUniqueName, postfix, } from "@aws-doc-sdk-examples/lib/utils/util-string.js"; import { sesClient } from "./libs/sesClient.js"; /** * Replace this with the name of an existing template. */ const TEMPLATE_NAME = getUniqueName("ReminderTemplate"); /** * Replace these with existing verified emails. */ const VERIFIED_EMAIL_1 = postfix(getUniqueName("Bilbo"), "@example.com"); const VERIFIED_EMAIL_2 = postfix(getUniqueName("Frodo"), "@example.com"); const USERS = [ { firstName: "Bilbo", emailAddress: VERIFIED_EMAIL_1 }, { firstName: "Frodo", emailAddress: VERIFIED_EMAIL_2 }, ]; /** * * @param { { emailAddress: string, firstName: string }[] } users * @param { string } templateName the name of an existing template in SES * @returns { SendBulkTemplatedEmailCommand } */ const createBulkReminderEmailCommand = (users, templateName) => { return new SendBulkTemplatedEmailCommand({ /** * Each 'Destination' uses a corresponding set of replacement data. We can map each user * to a 'Destination' and provide user specific replacement data to create personalized emails. * * Here's an example of how a template would be replaced with user data: * Template: <h1>Hello {{name}},</h1><p>Don't forget about the party gifts!</p> * Destination 1: <h1>Hello Bilbo,</h1><p>Don't forget about the party gifts!</p> * Destination 2: <h1>Hello Frodo,</h1><p>Don't forget about the party gifts!</p> */ Destinations: users.map((user) => ({ Destination: { ToAddresses: [user.emailAddress] }, ReplacementTemplateData: JSON.stringify({ name: user.firstName }), })), DefaultTemplateData: JSON.stringify({ name: "Shireling" }), Source: VERIFIED_EMAIL_1, Template: templateName, }); }; const run = async () => { const sendBulkTemplateEmailCommand = createBulkReminderEmailCommand( USERS, TEMPLATE_NAME, ); try { return await sesClient.send(sendBulkTemplateEmailCommand); } catch (err) { console.log("Failed to send bulk template email", err); return err; } };

要运行示例,请在命令提示符中键入以下内容。电子邮件会排队,等待由 Amazon SES 发送。

node ses_sendbulktemplatedemail.js

可以在此处找到此示例代码 GitHub。