繁体   English   中英

如何使用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格式创建替代正文,并且邮件将自动使用MIME类型multipart / alternative。 但是,根据AMP for Email的官方文档 ,我需要为AMP Emails设置一个全新的MIME类型: text/x-amp-html 我似乎找不到用PHPMailer做到这一点的方法。 我正在构建此脚本,因此以后可以在Magento 2上重新创建代码。目前,我仅发现该插件可以完全满足我的需要。 但是,我相信我尝试构建的PHP脚本对于整个Stackoverflow社区都应该是有用的。

我的最后一个想法是使用本机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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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