简体   繁体   中英

Use nodemailer to send AMP email

I recently came to know about AMP email and found it interesting and tried an example in nodejs using nodemailer package.

This package already contains an option 'amp' to send AMP specific emails, however I am unable to make it work.

The AMP version of email is really plain and it doesnt contain any xhr requests. Below is my code which sends an email to a specific email address.

  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);
  });

Also, I tried using "MailKit" package in C# but was not sure how to set an AMP version here. I added my AMP version under message.body section of MimeMessage class.

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

I am sure there is something really small that I am missing but unable to figure it out. Could someone take a look and guide me whats going wrong with above code?

AMP isn't RichText, it's HTML with a special Content-Type that is meant to be part of a multipart/alternative .

I would recommend reading https://amp.dev/documentation/guides-and-tutorials/learn/amp-email-format

To send AMP in MimeKit/MailKit, you would do something like this:

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;

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM