SendRawEmail与 AWS SDK 或 CLI 配合使用 - Amazon Simple Email Service

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

SendRawEmail与 AWS SDK 或 CLI 配合使用

以下代码示例演示如何使用 SendRawEmail

CLI
AWS CLI

使用 Amazon SES 发送原始电子邮件

以下示例使用 send-raw-email 命令发送带有 TXT 附件的电子邮件:

aws ses send-raw-email --raw-message file://message.json

输出:

{ "MessageId": "EXAMPLEf3f73d99b-c63fb06f-d263-41f8-a0fb-d0dc67d56c07-000000" }

原始消息是一个 JSON 数据结构,保存在当前目录下名为 message.json 的文件中。其中包含以下内容:

{ "Data": "From: sender@example.com\nTo: recipient@example.com\nSubject: Test email sent using the AWS CLI (contains an attachment)\nMIME-Version: 1.0\nContent-type: Multipart/Mixed; boundary=\"NextPart\"\n\n--NextPart\nContent-Type: text/plain\n\nThis is the message body.\n\n--NextPart\nContent-Type: text/plain;\nContent-Disposition: attachment; filename=\"attachment.txt\"\n\nThis is the text in the attachment.\n\n--NextPart--" }

如您所见,“Data”是一个长字符串,包含 MIME 格式的全部原始电子邮件内容,其中包括一个名为 attachment.txt 的附件。

请将 sender@example.com 和 recipient@example.com 替换为您要使用的地址。请注意,发件人的电子邮件地址必须已通过 Amazon SES 验证。在您获得 Amazon SES 的生产访问权限之前,您还必须验证收件人的电子邮件地址,除非收件人是 Amazon SES 邮箱模拟器。有关验证的更多信息,请参阅《Amazon Simple Email Service 开发人员指南》中的“在 Amazon SES 中验证电子邮件地址和域”。

输出中的消息 ID 表示对的调用 send-raw-email 成功。

如果您没有收到电子邮件,请检查垃圾邮件。

有关发送原始电子邮件的更多信息,请参阅《Amazon Simple Email Service 开发人员指南》中的“使用 Amazon SES API 发送原始电子邮件”。

  • 有关 API 的详细信息,请参阅AWS CLI 命令参考SendRawEmail中的。

JavaScript
适用于 JavaScript (v3) 的软件开发工具包
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整实例,了解如何进行设置和运行。

使用 nodemailer 发送带附件的电子邮件。

import sesClientModule from "@aws-sdk/client-ses"; /** * nodemailer wraps the SES SDK and calls SendRawEmail. Use this for more advanced * functionality like adding attachments to your email. * * https://nodemailer.com/transports/ses/ */ import nodemailer from "nodemailer"; /** * @param {string} from An Amazon SES verified email address. * @param {*} to An Amazon SES verified email address. */ export const sendEmailWithAttachments = ( from = "from@example.com", to = "to@example.com", ) => { const ses = new sesClientModule.SESClient({}); const transporter = nodemailer.createTransport({ SES: { ses, aws: sesClientModule }, }); return new Promise((resolve, reject) => { transporter.sendMail( { from, to, subject: "Hello World", text: "Greetings from Amazon SES!", attachments: [{ content: "Hello World!", filename: "hello.txt" }], }, (err, info) => { if (err) { reject(err); } else { resolve(info); } }, ); }); };
  • 有关 API 的详细信息,请参阅 AWS SDK for JavaScript API 参考SendRawEmail中的。

有关 S AWS DK 开发者指南和代码示例的完整列表,请参阅将 Amazon SES 与 AWS 软件开发工具包一起使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。