加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php mailer和html包含php变量

发布时间:2020-12-13 16:31:49 所属栏目:PHP教程 来源:网络整理
导读:你好我正在尝试使用php mailer类发送html电子邮件.问题是我想在我的电子邮件中包含php变量,同时使用include来保持组织.我的php邮件 $place = $data['place']; $start_time = $data['start_time'];$mail-IsHTML(true); // set email format to HTML$mail-Subj
你好我正在尝试使用php mailer类发送html电子邮件.问题是我想在我的电子邮件中包含php变量,同时使用include来保持组织.我的php邮件
$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)..

<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和一个简短的帮手功能:
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函数由PHP include documentation提供,修改稍微包含一个变量数组.
>重要提示:由于您的包含在函数中进行处理,因此PHP模板文件(/emails/event.php)的执行范围是该函数的范围(除super globals之外,没有变量立即可用)
>这就是为什么我添加了extract($variablesToMakeLocal) – 它从$variablesToMakeLocal中提取所有数组键作为函数范围中的变量,这反过来意味着它们在包含文件的范围内.

由于你已经在$data数组中有place和start_time,所以我简单地将它直接传递给函数.您可能想要注意,这将提取$数据中的所有键 – 您可能或可能不希望.>请注意,现在您的模板文件正在处理为一个PHP文件,因此所有相同的警告和语法规则都适用.您不应该将其公开以供外界编辑,您必须使用<?php echo $place?>输出变量,如同任何PHP文件一样.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读