使用nodemailer发送AMP电子邮件

时间:2019-05-07 13:09:35

标签: node.js c#-4.0 nodemailer mailkit amp-email

我最近开始了解AMP电子邮件,并发现它很有趣,并使用nodemailer软件包在nodejs中尝试了一个示例。

该软件包已经包含一个用于发送AMP特定电子邮件的“ amp”选项,但是我无法使其正常工作。

电子邮件的AMP版本确实很简单,并且不包含任何xhr请求。 下面是我的代码,该代码将电子邮件发送到特定的电子邮件地址。

  let transporter = nodeMailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    secure: true,
    auth: {
      user: "validuser@gsuiteBusinessdomain.com",
      pass: "validpassword"
  },
  });

  let mailOptions = {
    from: 'sender@gsuiteBusinessdomain.com', // sender address
    to: "enduser@gsuiteBusinessdomain.com", // list of receivers
    subject: "test subject", // Subject line
    text: " basic text body example ", // plain text body
    html: '<b>basic html body example</b>', // html body
    amp: `<!--
        Below is the mininum valid AMP4EMAIL document. Just type away
        here and the AMP Validator will re-check your document on the fly.
   -->
   <!doctype html>
   <html ⚡4email>
   <head>
     <meta charset="utf-8">
     <script async src="https://cdn.ampproject.org/v0.js"></script>
     <style amp4email-boilerplate>body{visibility:hidden}</style>
   </head>
   <body>
     Hello, AMP4EMAIL world.
   </body>
   </html>`
  };

  transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
      console.log(error);
    }
    console.log('Message %s sent: %s', info.messageId, info.response);
  });

此外,我尝试在C#中使用“ MailKit”软件包,但不确定如何在此处设置AMP版本。我在message.body部分下添加了我的AMP版本  MimeMessage类。

 message.Body = new TextPart(TextFormat.RichText)
            {
                Text = @"<!-- ........

我敢肯定我确实缺少一些小东西,但无法弄清楚。 有人可以看一下并指导我上面的代码怎么了吗?

1 个答案:

答案 0 :(得分:2)

AMP不是RichText,它是带有特殊Content-Type的HTML,它是multipart/alternative的一部分。

我建议阅读https://amp.dev/documentation/guides-and-tutorials/learn/amp-email-format

要在MimeKit / MailKit中发送AMP,您需要执行以下操作:

var alternative = new MultipartAlternative ();
alternative.Add (new TextPart ("plain") {
    Text = "This is the plain-text message body."
});

// Note: Some email clients[1] will only render the last MIME part, so we
// recommend placing the text/x-amp-html MIME part before the text/html
// MIME part.
alternative.Add (new TextPart ("x-amp-html") {
    Text = @"<!--
    Below is the minimum valid AMP4EMAIL document. Just type away
    here and the AMP Validator will re-check your document on the fly.
-->
<!doctype html>
<html ⚡4email>
<head>
  <meta charset=""utf-8"">
  <script async src=""https://cdn.ampproject.org/v0.js""></script>
  <style amp4email-boilerplate>body{visibility:hidden}</style>
</head>
<body>
  Hello, AMP4EMAIL world.
</body>
</html>"
});

alternative.Add (new TextPart ("html") {
    Text = "This is the <b>html</b> message body."
});

message.Body = alternative;

希望有帮助。