Search code examples
phpphpmaileramp-htmlamp-email

How to send AMP Emails with PHP?


I am trying to create a PHP script to send AMP Emails with PHPMailer. When reading online tutorial I found that you can specify Mime types in PHPMailer as follows:

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

This should create alternative body in TEXT format and the message will automatically use the MIME type multipart/alternative. However, according to the AMP for Email 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 that should do exactly what I need. However, I believe this PHP script that I am trying to build, should be useful for the whole Stackoverflow community.

My last idea was to send AMP Email using native PHP mail() function, but I don’t know how. I think, I must pass AMP Email HTML in $message variable and set AMP headers in $headers. Please see below:

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

Any help appreciated!


Solution

  • The following script should add one additional mime type to your email. I have followed both of your links to understand what you need and built this snippet according to the documentation provided. But I didn't have time to test it. Hope it helps.

    //specify the email address you are sending to, and the email subject
    $email = '[email protected]';
    $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);