You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PHP发送AMP邮件?PHPMailer及原生mail()实现疑问

刚好我之前处理过AMP邮件发送的需求,给你分享PHPMailer和原生PHP mail()两种方案的具体实现方法,亲测有效!

使用PHPMailer发送AMP邮件

PHPMailer本身没有专门的API来直接设置text/x-amp-html类型的内容,但我们可以通过手动添加MIME部分来实现——毕竟AMP邮件要求在multipart/alternative容器里同时包含纯文本、普通HTML、AMP HTML三个内容块。

具体步骤和代码示例如下:

  • 先完成PHPMailer的基础配置(SMTP、收发件人信息等)
  • 设置常规的HTML内容和纯文本替代内容
  • 用addPart()方法手动添加AMP HTML部分,指定MIME类型为text/x-amp-html
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
require 'PHPMailer/src/Exception.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

$mail = new PHPMailer(true);

try {
    // 配置SMTP服务器信息
    $mail->isSMTP();
    $mail->Host = 'your-smtp-host.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'your-email@example.com';
    $mail->Password = 'your-smtp-password';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = 587;

    // 设置发件人和收件人
    $mail->setFrom('from@example.com', 'Your Brand Name');
    $mail->addAddress('recipient@example.com', 'Recipient Name');

    // 邮件主题
    $mail->Subject = 'Test AMP Email via PHPMailer';

    // 普通HTML内容(作为AMP不支持时的 fallback)
    $regularHtml = '<!DOCTYPE html><html><body><h1>Hello there! This is regular HTML content.</h1></body></html>';
    $mail->Body = $regularHtml;

    // 纯文本替代内容
    $mail->AltBody = 'Hello, my friend! This is the plain text version of your message.';

    // 符合AMP规范的HTML内容
    $ampHtml = '<!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>
        <h1>Hello! This is your AMP-powered email content.</h1>
    </body>
    </html>';
    // 添加AMP内容部分,指定MIME类型
    $mail->addPart($ampHtml, 'text/x-amp-html');

    // 发送邮件
    $mail->send();
    echo 'AMP email sent successfully!';
} catch (Exception $e) {
    echo "Message couldn't be sent. Error: {$mail->ErrorInfo}";
}
使用原生PHP mail()函数发送AMP邮件

如果想用原生mail()函数,就得手动构建完整的MIME邮件结构,核心是正确设置MIME边界,并依次添加三个内容部分。

代码示例:

$to = 'recipient@example.com';
$subject = 'Test AMP Email via Native PHP mail()';
$from = 'your-email@example.com';

// 生成唯一的MIME边界字符串
$mimeBoundary = uniqid('amp-mail-boundary-');

// 构建邮件头部
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: {$from}\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"\r\n";

// 准备三个内容块
$plainTextContent = "Hello, my friend! This is the plain text fallback for your email.";
$regularHtmlContent = '<!DOCTYPE html><html><body><h1>Hello there! This is regular HTML content.</h1></body></html>';
$ampHtmlContent = '<!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>
    <h1>Hello! This is your AMP-powered email content.</h1>
</body>
</html>';

// 拼接完整的邮件内容
$message = "--{$mimeBoundary}\r\n";
$message .= "Content-Type: text/plain; charset=utf-8\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= $plainTextContent . "\r\n\r\n";

$message .= "--{$mimeBoundary}\r\n";
$message .= "Content-Type: text/html; charset=utf-8\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= $regularHtmlContent . "\r\n\r\n";

$message .= "--{$mimeBoundary}\r\n";
$message .= "Content-Type: text/x-amp-html; charset=utf-8\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= $ampHtmlContent . "\r\n\r\n";

// 结束MIME边界
$message .= "--{$mimeBoundary}--";

// 发送邮件
if (mail($to, $subject, $message, $headers)) {
    echo 'AMP email sent successfully!';
} else {
    echo 'Failed to send the email.';
}

额外注意事项

  • 你的AMP内容必须严格遵循AMP for Email规范,比如必须包含<html ⚡4email>标签、加载官方AMP脚本、添加boilerplate样式,否则邮件客户端会拒绝渲染AMP内容。
  • 部分邮箱服务商(比如Gmail、Outlook)需要你先验证发件域名并提交AMP邮件使用申请,才能正常显示AMP内容。
  • 对于Magento 2的重构,你可以基于PHPMailer的方案,扩展Magento自带的邮件发送类,添加AMP内容的支持逻辑。

内容的提问来源于stack exchange,提问作者Petro Chaikivskyi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:06:09