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 = "[email protected]";
 $mail->FromName = "mail.com";
 $mail->addAddress($email);
 $mail->addReplyTo("[email protected]", "Reply");
 $mail->isHTML(true);

 $mail->Subject = "Details";
 $mail->Body = file_get_contents('epay.html');
 $mail->Send();
2

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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;

4 Comments

what is the return <<< END for
<<<END is heredoc. Look here php.net/manual/en/language.types.string.php. The return statement will return the content to $mail->body
the mail doesnt send with this format. been waiting to get a mail for over an hour
-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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.