Используйте nodemailer для отправки электронной почты AMP - PullRequest
1 голос
/ 07 мая 2019

Недавно я узнал об электронной почте AMP, нашел ее интересной и попробовал пример в nodejs с использованием пакета nodemailer.

Этот пакет уже содержит опцию '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);
  });

Также я попытался использовать пакет «MailKit» в C #, но не знал, как установить версию AMP здесь. Я добавил свою версию AMP в раздел message.body класса MimeMessage.

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

Я уверен, что есть кое-что действительно маленькое, что мне не хватает, но я не могу понять это. Может кто-нибудь взглянуть и подсказать, что не так с кодом выше?

1 Ответ

2 голосов
/ 07 мая 2019

AMP - это не RichText, это HTML со специальным типом контента, который должен быть частью multipart/alternative.

. Я бы рекомендовал прочитать https://amp.dev/documentation/guides-and-tutorials/learn/amp-email-format

Для отправкиAMP в MimeKit / MailKit, вы бы сделали что-то вроде этого:

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;

Надеюсь, что поможет.

...