与 AWS SDK或SendRawEmail一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

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

与 AWS SDK或SendRawEmail一起使用 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" }

原始消息是保存在当前目录中名为的文件message.json中的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--" }

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

请将 sender@example.com 和 recipient@example.com 替换为您要使用的地址。请注意,发件人的电子邮件地址必须通过 Amazon 进行验证SES。除非收件人是亚马逊邮箱模拟器SES,否则您还必须验证收件人的电子邮件地址,除非收件人是亚马逊SES邮箱模拟器。有关验证的更多信息,请参阅《亚马逊简单电子邮件服务开发者指南》中的 “SES在亚马逊中验证电子邮件地址和域名”。

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

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

有关发送原始电子邮件的更多信息,请参阅《亚马逊简单电子邮件服务开发者指南》SESAPI中的使用亚马逊发送原始电子邮件

JavaScript
SDK对于 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” 中的。