如何发送 PHP 的 AMP 电子邮件?

How to send AMP Emails with PHP?

我正在尝试创建一个 PHP 脚本来使用 PHPMailer 发送 AMP 电子邮件。在阅读在线教程时,我发现您可以在 PHPMailer 中指定 Mime 类型,如下所示:

$mail->AltBody = "Hello, my friend! This message uses plain text !";

这应该以 TEXT 格式创建替代 body,消息将自动使用 MIME 类型 multipart/alternative。但是,根据电子邮件 official documentation, I need to set a completely new MIME type for AMP Emails: text/x-amp-html. I can’t seem to find a way to do that with PHPMailer. I am building this script so I later can re-create the code on Magento 2. For now I only found this plugin 的 AMP,这应该完全符合我的需要。但是,我相信我正在尝试构建的这个 PHP 脚本应该对整个 Whosebug 社区有用。

我最后的想法是使用本机 PHP mail() 函数发送 AMP 电子邮件,但我不知道如何。我想,我必须在 $message 变量中传递 AMP 电子邮件 HTML,并在 $headers 中设置 AMP headers。请看下面:

mail($to, $subject, $message, $headers);

感谢任何帮助!

以下脚本应向您的电子邮件添加一种额外的 MIME 类型。我已经关注了您的两个链接以了解您的需求并根据提供的文档构建了此代码段。但是我没有时间去测试它。希望对你有帮助。

//specify the email address you are sending to, and the email subject
$email = 'email@example.com';
$subject = 'Email Subject';

//create a boundary for the email. This 
$boundary = uniqid('np');

//headers - specify your from email address and name here
//and specify the boundary for the email
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: Your Name \r\n";
$headers .= "To: ".$email."\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";

//here is the content body
$message = "This is a MIME encoded message.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";

$message .= "Content-type: text/plain;charset=utf-8\r\n\r\n"
//Plain text body
$message .= "Hello,\nThis is a text email, the text/plain version.
\n\nRegards,\nYour Name";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";

//Html body
$message .= "
 Hello,
This is a text email, the html version.

Regards,
Your Name";
$message .= "\r\n\r\n--" . $boundary . "--";
$message .= "Content-type: text/x-amp-html;charset=utf-8\r\n\r\n"

//AMP Email body
$message .= ‘<!doctype html>
<html ⚡4email>
<head>
  <meta charset="utf-8">
  <style amp4email-boilerplate>body{visibility:hidden}</style>
  <script async src="https://cdn.ampproject.org/v0.js"></script>
</head>
<body>
Hello World in AMP!
</body>
</html>’;
$message .= "\r\n\r\n--" . $boundary . "\r\n";

//invoke the PHP mail function
mail('', $subject, $message, $headers);