javascript - 在 AWS-sdk Nodejs 中列出取消订阅

标签 javascript node.js amazon-ses aws-sdk-js aws-sdk-nodejs

如何添加 List-Unsubsribe : <mailto:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f7e7d7c5f7c7b79317c7072" rel="noreferrer noopener nofollow">[email protected]</a>>使用 Amazon SES(简单电子邮件服务)时我的外发电子邮件的 header ?我正在使用 AWS 的 JavaScript SDK。

以下是我查看过但未能成功找到答案的各种文档链接:Link1 , Link2 , Link3 , Link4 .

我尝试过使用 SendMail 和 SendRawEmail API。

使用 SendMail API

let params={
 Source: auth.host, 
 Destination: { ToAddresses: [email] },
 Headers:{ListUnsubscribe :'<mailto:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e3f3c3d1e3a3b38703d3133" rel="noreferrer noopener nofollow">[email protected]</a>>'},  //error unexpected key Headers
 Message: {Subject:{Data: subject },Body:{Text: {Data: mail}}} 
}

ses.sendEmail(params,(err, data)=>{
    if(err){
      console.error(err);
    }else{
      console.log('Email sent: ');
      console.log(data);

    }
});  

使用 SendRawEmail API

var mailOptions = {
      from: auth.host,
      subject: subject,
      text: mail,
      to: email,
    };

    var mail = mailcomposer(mailOptions);

    mail.build(function (err, message){
      var req = ses.sendRawEmail({RawMessage: {Data: message}});

      req.on('build', function() {
          req.httpRequest.headers["List-Unsubscribe"] = "<mailto:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b3a39381b3a393875383436" rel="noreferrer noopener nofollow">[email protected]</a>>";
      });

      req.send(function (err, data) {
          if(err) //code
          else  //code
      });
    });

最佳答案

根据 AWS 文档,您只能使用 SendRawEmail API 指定自定义 header :

The SendRawEmail API provides you the flexibility to format and send your own raw email message by specifying headers, MIME parts, and content types. SendRawEmail is typically used by advanced users. You need to provide the body of the message and all header fields that are specified as required in the Internet Message Format specification (RFC 5322). For more information, see Sending Raw Email Using the Amazon SES API.

这解释了为什么您的第一个示例不起作用。凉爽的。


这就是为什么您的第二个示例(实际上使用 SendRawEmail API)不起作用。

不幸的是,为了在发送电子邮件时指定自定义 header ,您必须在原始消息正文中内联指定它们。在制作此原始消息时,您必须遵循一些相当严格的格式要求,否则您的传递可能会失败/输出不正确。尽管这非常有帮助,但您没有可以使用的 api 方法来让您使用键/值对来指定 header 。

此限制可能是由于 AWS 必须解码您的 base64 编码的原始消息(可以是 Buffer、blob、类型化数组、字符串中的任何类型),希望您正确格式化消息,并插入您的消息标题位于正确的位置。毫无疑问,这对 API 的最终用户很有帮助,但对于他们来说,他们必须执行的所有验证和错误处理绝对是一场噩梦。

以下是使用 SendRawEmail API 发送电子邮件的详细要求:

RawMessage — (map) The raw text of the message. The client is responsible for ensuring the following:

Message must contain a header and a body, separated by a blank line. All required header fields must be present. Each part of a multipart MIME message must be formatted properly. MIME content types must be among those supported by Amazon SES. For more information, go to the Amazon SES Developer Guide. Must be base64-encoded. Per RFC 5321, the maximum length of each line of text, including the , must not exceed 1,000 characters. Data — required — (Buffer, Typed Array, Blob, String) The raw data of the message. This data needs to base64-encoded if you are accessing Amazon SES directly through the HTTPS interface. If you are accessing Amazon SES using an AWS SDK, the SDK takes care of the base 64-encoding for you. In all cases, the client must ensure that the message format complies with Internet email standards regarding email header fields, MIME types, and MIME encoding.

The To:, CC:, and BCC: headers in the raw message can contain a group list.

If you are using SendRawEmail with sending authorization, you can include X-headers in the raw message to specify the "Source," "From," and "Return-Path" addresses. For more information, see the documentation for SendRawEmail.

Do not include these X-headers in the DKIM signature, because they are removed by Amazon SES before sending the email. For more information, go to the Amazon SES Developer Guide.


我的两分钱

这看起来确实很不方便。我建议使用像 Sendgrid 这样的服务,它内置了订阅管理(订阅/取消订阅)、模板管理等功能,以及现代专用电子邮件服务应具备的一大堆功能。但是,如果您别无选择,那么也许会喜欢 SendRawEmail API 为您提供大量自定义功能,但代价是有点乏味。

您可以从 AWS SES Javascript API docs 访问此信息及更多信息。 :

关于javascript - 在 AWS-sdk Nodejs 中列出取消订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49168291/

相关文章:

javascript - 我的代码在本地和在 codepen 或 plunker 中的执行方式不同

javascript - 在路由组件之外 react 路由器重定向

javascript - 浏览器编程是否标准化,所有主要浏览器是否都符合标准?

node.js - RethinkDB 与 NodeJS : Open individual connections vs single static connection

amazon-web-services - 使用Amazon SES服务重定向电子邮件

JavaScript 全局执行上下文

node.js - 将前端 native websocket 客户端连接到 Node.js Socket.io 服务器

node.js - 如何将多个参数传递给 npm start

amazon-web-services - 从 AWS SES 发送附件小于 10 MB 的电子邮件

node.js - 在不超过速率限制的情况下尽可能快地通过 SES 发送电子邮件