简体   繁体   中英

Trying to send a responsive email using PHP mailer

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>';
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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