php mailer和html包含php变量 [英] php mailer and html includes with php variables
问题描述
您好,我正在尝试使用php mailer类发送html电子邮件.问题是我想在使用include时将php变量包含在我的电子邮件中,以使事情井井有条.这是我的php mailer....
Hello I am trying to send html emails using php mailer class. The problem is i would like to incllude php variables in my email while using includes as to keep things organized. Heres my php mailer....
$place = $data['place'];
$start_time = $data['start_time'];
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "You have an event today";
$mail->Body = file_get_contents('../emails/event.html');
$mail->Send(); // send message
我的问题是,是否可以在event.html中使用php变量?我没有运气尝试过(下面是event.html).
my question is, is it possible to have php variables in event.html ? i tried this with no luck (below is event.html)..
<table width='600px' cellpadding='0' cellspacing='0'>
<tr><td bgcolor='#eeeeee'><img src='logo.png' /></td></tr>
<tr><td bgcolor='#ffffff' bordercolor='#eeeeee'>
<div style='border:1px solid #eeeeee;font-family:Segoe UI,Tahoma,Verdana,Arial,sans-serif;padding:20px 10px;'>
<p style=''>This email is to remind you that you have an upcoming meeting at $place on $start_time.</p>
<p>Thanks</p>
</div>
</td></tr>
</table>
推荐答案
是的,很容易使用 include 和一个简短的帮助程序功能:
Yes, very easily with include and a short helper function:
function get_include_contents($filename, $variablesToMakeLocal) {
extract($variablesToMakeLocal);
if (is_file($filename)) {
ob_start();
include $filename;
return ob_get_clean();
}
return false;
}
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "You have an event today";
$mail->Body = get_include_contents('../emails/event.php', $data); // HTML -> PHP!
$mail->Send(); // send message
-
get_include_contents
函数由 PHPinclude
文档提供,对其进行了少许修改,以包含变量数组.The
get_include_contents
function is courtesy of the PHPinclude
documentation, modified slightly to include an array of variables.重要提示::由于您的包含在函数中进行处理,因此PHP模板文件(
/emails/event.php
)的执行范围在该函数的范围内(除了超级全局变量Important: Since your include is processing within a function, the scope of execution of the PHP template file (
/emails/event.php
) is in that function's scope (no variables immediately available besides super globals这就是为什么我添加了
extract($variablesToMakeLocal)
—的原因它从$variablesToMakeLocal
中提取所有数组键作为函数范围内的变量,这又意味着它们在所包含文件的范围之内.That is why I have added
extract($variablesToMakeLocal)
— it extracts all array keys from$variablesToMakeLocal
as variables in the function's scope, which in turn means they are within scope of the file being included.由于您已经在
$data
数组中包含了place
和start_time
,因此我直接将其直接传递给了函数.您可能需要注意,这将提取$data
—中的所有键.您可能会或可能不会想要.Since you already had
place
andstart_time
in the$data
array, I simply passed that straight into the function. You may want to be aware that this will extract all keys within$data
— you may or may not want that.请注意,现在您的模板文件正在作为PHP文件处理,因此所有相同的警告和语法规则适用.您应该不将其公开以供外界编辑,并且必须使用
<?php echo $place ?>
来输出变量,就像在任何PHP文件中一样.Note that now your template file is processing as a PHP file, so all the same caveats and syntax rules apply. You should not expose it to be edited by the outside world, and you must use
<?php echo $place ?>
to output variables, as in any PHP file.这篇关于php mailer和html包含php变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!