繁体   English   中英

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

[英]Pass PHP variable to html email

正在使用与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.

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

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

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

epay.php:

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

例如:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM