【问题标题】:Use nodemailer to send AMP email使用 nodemailer 发送 AMP 电子邮件
【发布时间】:2019-05-07 13:09:35
【问题描述】:

我最近了解了 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 = @"<!-- ........

我确信有一些非常小的东西我错过了但无法弄清楚。 有人可以看看并指导我上面的代码出了什么问题吗?

【问题讨论】:

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


    【解决方案1】:

    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;
    

    希望对您有所帮助。

    【讨论】:

    • 感谢@jstedfast 的回复。我应该在之前提到它。我什至使用了 HTML 格式,但这并没有给我一个 AMP 页面,而只是一个 HTML 页面
    • 我认为您对 AMP 电子邮件到底是什么感到困惑(或者我可能是?)。根据您将消息正文设置为的文本,即 HTML。因此,当您告诉我“是的,但它显示为 HTML”时,我认为“嗯,呃,因为它 ”。你为什么期待不同的东西?
    • 没关系,我看到了问题,你需要一个特殊的 mime 类型。我会更新我的答案。
    • 是的,这正是我想要的。谢谢你指引我正确的道路
    猜你喜欢
    • 2015-05-19
    • 1970-01-01
    • 2022-07-27
    • 1970-01-01
    • 2018-07-14
    • 2018-07-11
    相关资源
    最近更新 更多