php mailer和html包含php变量
你好我正在尝试使用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 由于你已经在$data数组中有place和start_time,所以我简单地将它直接传递给函数.您可能想要注意,这将提取$数据中的所有键 – 您可能或可能不希望.>请注意,现在您的模板文件正在处理为一个PHP文件,因此所有相同的警告和语法规则都适用.您不应该将其公开以供外界编辑,您必须使用<?php echo $place?>输出变量,如同任何PHP文件一样. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |