0

am sending an html mail to my users using inlined css mixed with the html. The problem am having is how to pass my variables from my php to my html and use it there accordingly as i send it as an email. eg the mail is meant to contain user full name, user purchase and amount of purchase. how do i pass that to my html

 $mail = new PHPMailer;

 $mail->From = "mail@mail.com";
 $mail->FromName = "mail.com";
 $mail->addAddress($email);
 $mail->addReplyTo("mail@mail.com", "Reply");
 $mail->isHTML(true);

 $mail->Subject = "Details";
 $mail->Body = file_get_contents('epay.html');
 $mail->Send();
nsikak
  • 111
  • 9

3 Answers3

2

I would suggest you to use a templating engine like smarty (http://www.smarty.net/). Here is an example (copy pasted from their documentations as an example of usage):

Variables assigned from PHP

Assigned variables that are referenced by preceding them with a dollar ($) sign.

Example 4.2. Assigned variables

<?php

$smarty = new Smarty();

$smarty->assign('firstname', 'Doug');
$smarty->assign('lastname', 'Evans');
$smarty->assign('meetingPlace', 'New York');

$smarty->display('index.tpl');

?>

index.tpl source:

Hello {$firstname} {$lastname}, glad to see you can make it.
<br />
{* this will not work as $variables are case sensitive *}
This weeks meeting is in {$meetingplace}.
{* this will work *}
This weeks meeting is in {$meetingPlace}.

This above would output:

Hello Doug Evans, glad to see you can make it.
<br />
This weeks meeting is in .
This weeks meeting is in New York.

continue reading here http://www.smarty.net/docs/en/language.variables.tpl

Ben Yitzhaki
  • 1,246
  • 16
  • 29
0

You should use a template instead html file. If you don't want to use Smarty you could use a PHP file instead. Example:

$amount = 210.14;
$mail->Body = require('epay.php');

epay.php:

<?php
return <<<END
<div class="amount">{$amount}</div>
END;
Pavel Tzonkov
  • 227
  • 3
  • 8
-1

For example:

$ext = 'Some value';
$body = require('body_template.php');

$mail->Body = $body;
$mail->Send();

body_template.php

$template = <<<TMPL
<p>Some value: {$ext}</p>
TMPL;

return $template;
buildok
  • 775
  • 6
  • 7