Trying to send a responsive email using PHP mailer

走远了吗. 提交于 2019-12-02 22:38:56

问题


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 ->isSMTP();
$m->SMTPAuth=true;

// debugging
// $m->SMTODebug=1
// endof debug
$m->Host="smtp.gmail.com";
$m->Username="example@gmail.com";
$m->Password="blahblah";
$m->SMTPSecure='ssl';
$m->Port=465;
$m->isHtml(true);

$m->Subject = 'Welcome to Efie';
$m->msgHTML(file_get_contents('functions/register-email.php'), dirname(__FILE__));
$m->FromName="Contact Form Efie";
$m->AddAddress($email,$fname);
if($m->send()) {
    echo '<p class="errors bg-success text-success">Email   Received</p>';
}

回答1:


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.



来源:https://stackoverflow.com/questions/39265928/trying-to-send-a-responsive-email-using-php-mailer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!