Trying to send a responsive email using PHP mailer

后端 未结 1 1050
北海茫月
北海茫月 2021-01-29 11:25

I have a responsive email template in a php file and trying to send it with PHP mailer with no success. My code looks like this.

$m = new PHPMailer;
$m ->isSM         


        
相关标签:
1条回答
  • 2021-01-29 11:30

    This isn't anything to do with it being responsive - that's just a matter of using the CSS media queries in the Zurb CSS, it doesn't need any javascript.

    The problem you're seeing is that file_get_contents literally gets the contents of the file, it does not run it as a PHP script. There are several ways to solve this.

    You can include the file while assigning it to a variable, like this:

    $body = include 'functions/register-email.php';
    $m->msgHTML($body, dirname(__FILE__));
    

    The problem with this approach is that you can't just have content sitting in the file, you need to return it as a value, so your template would be something like:

    <?php
    $text = <<<EOT
    <html>
    <body>
    <h1>$headline</h1>
    </body>
    </html>
    EOT;
    return $text;
    

    An easier approach is to use output buffering, which makes the template file simpler:

    ob_start();
    include 'functions/register-email.php';
    $body = ob_get_contents();
    ob_end_clean();
    $m->msgHTML($body, dirname(__FILE__));
    

    and the template would be simply:

    <html>
    <body>
    <h1><?php echo $headline; ?></h1>
    </body>
    </html>
    

    Either way, the template file will have access to your local variables and interpolation will work.

    There are other options such as using eval, but it's inefficient and easy to do things wrong.

    Using output buffering is the simplest, but if you want lots more flexibility and control, use a templating language such as Smarty or Twig.

    For working with Zurb, you really need a CSS inliner such as emogrifier to post-process your rendered template, otherwise things will fall apart in gmail and other low-quality mail clients.

    FYI, this stack - Zurb templates, Smarty, emogrifier, PHPMailer - is exactly what's used in smartmessages.net, which I built.

    0 讨论(0)
提交回复
热议问题