Kofi Amoussou Kofi Amoussou - 1 year ago 139
PHP Question

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();

// debugging
// $m->SMTODebug=1
// endof debug

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

Answer Source

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:

$text = <<<EOT
return $text;

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

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

and the template would be simply:

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

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download