php 邮件程序和 html 包含 php 变量
您好,我正在尝试使用 php 邮件程序类发送 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>
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
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,使用 include 和一个简短的辅助函数非常容易:
get_include_contents 函数由 PHP
include
文档提供< /a>,稍作修改以包含数组变量。重要提示:由于您的包含是在函数内处理的,因此 PHP 模板文件 (
/emails/event.php
) 的执行范围位于该函数的范围内(除了超级全局变量之外,没有立即可用的变量
这就是为什么我 可以立即使用已添加
extract($variablesToMakeLocal)
— 它从$variablesToMakeLocal
中提取所有数组键作为函数作用域中的变量,这意味着它们位于所包含文件的作用域内。< /p>由于您在
$data
数组中已经有了place
和start_time
,我只是将其直接传递到函数中。您可能想知道这将提取$data
中的所有键 - 您可能想要也可能不想要。请注意,现在您的模板文件正在作为 PHP 文件进行处理,因此所有相同的注意事项和语法规则都适用。您不应该将其暴露给外界编辑,并且必须使用
来输出变量,就像在任何 PHP 文件中一样.
Yes, very easily with include and a short helper function:
The
get_include_contents
function is courtesy of the PHPinclude
documentation, modified slightly to include an array of variables.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 globalsThat 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.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.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.有几种方法可以做到这一点:
令牌模板
令牌邮件程序
缓冲区模板
缓冲区邮件程序
Couple ways to do it:
Token Template
Token Mailer
Buffer Template
Buffer Mailer
您可以将变量放入 html 电子邮件中,然后执行
string_replace
,以便内容而不是变量出现在电子邮件中:这是对我有用的设置。也许它不干燥,但它有效。
You can put variables in the html email and then do a
string_replace
so the contents appear in the email instead of the variables:This is the setup that worked for me. It not DRY perhaps, but it works.
使用 prodigitalson 的令牌方法,以下内容对我有用。 PHP 代码是:
HTML 只是:
在我的例子中,
eregi_replace
比strtr
效果更好 - (后者根本不起作用)。Using prodigitalson's token method, the following worked for me. PHP code was:
The HTML was just:
The
eregi_replace
worked better thatstrtr
in my case - (the latter didn't work at all).也许有点晚了,但这是我经常使用的方法。
(在 stackoverflow 上找到了这个,但找不到它的链接。所以这个解决方案的积分不适合我!)
首先要做的就是替换 html 电子邮件中的变量,如下所示:
然后在 PHP 中,您首先获取内容邮件模板并将其分配给一个变量,比如说$mailBody。然后使用您想要插入到 HTML 电子邮件模板中的变量创建一个新数组。设置后,您可以使用循环来获取电子邮件内的正确变量。
这样你只需几行额外的代码就可以保持一切干净。
我什至用它来发送包含所有不同正文和大量变量的批量电子邮件,并且运行速度相当快。
希望有帮助。
Maybe a little bit late to the party, but here is the method I always use.
(Found this one somewhere on stackoverflow but can't find the link to it. so credits for this solution are not for me!)
First thing to do is replace the variables in html email like so:
Then in PHP you first get the contents of the mailtemplate and assign it to a variable, lets say $mailBody. Then make a new array with the variables you would like to insert in the HTML email template. Once set, you can use a loop to get correct varibales inside the email.
This way you can keep everything clean with just a few lines of extra code.
I've used this even to send bulk emails with all diffrent body's and a lot of variables and it runs fairly quick.
Hope it helps.