将PHP变量传递给html电子邮件

时间:2017-06-27 07:16:57

标签: php html css

使用与html混合的内联css向我的用户发送html邮件。 我遇到的问题是如何将我的变量从我的PHP传递到我的html并相应地使用它,因为我将其作为电子邮件发送。例如,邮件旨在包含用户全名,用户购买和购买金额。我如何将其传递给我的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();

3 个答案:

答案 0 :(得分:2)

我建议你使用像smarty(http://www.smarty.net/)这样的模板引擎。以下是一个示例(从文档中粘贴的副本作为使用示例):

从PHP分配的变量

通过前面带有美元($)符号引用的指定变量。

例4.2。分配变量

<?php

$smarty = new Smarty();

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

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

?>

index.tpl来源:

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}.

以上将输出:

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

继续阅读http://www.smarty.net/docs/en/language.variables.tpl

答案 1 :(得分:0)

您应该使用模板而不是html文件。如果您不想使用Smarty,则可以使用PHP文件。例如:

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

epay.php:

<?php
return <<<END
<div class="amount">{$amount}</div>
END;

答案 2 :(得分:-1)

例如:

$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;