我正在使用与 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();
最佳答案
我建议您使用像 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.
关于php - 将 PHP 变量传递给 html 电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44774326/