使用模板文件发送电子邮件
我正在尝试找出从外部模板文件发送电子邮件的最佳方式,目前我有一个如下所示的模板文件:
Thank you, your order has been received, someone will review it and process it. No money has been taken from your account.
<?php
echo date('Y-m-d H:i:s');
?>
<pre>
<?php print_r($this->data); ?>
</pre>
然后我的发送方法如下所示:
public function notify($template) {
// get the template from email folder
$path = $_SERVER['DOCUMENT_ROOT'].'templates/email/'.$template.'.php';
if(file_exists($path)) {
ob_start();
require_once($path);
$body = ob_get_contents();
ob_end_clean();
$subject = 'email send';
foreach($this->emailTo as $email)
new Mail($email,$subject,$body);
}
}
当我像这样调用它时,一切正常:
$notifications = new notifications();
$notifications->setData(array('order' => $order->order));
$notifications->addEmail($order->order->email);
$notifications->notify('orderReceived');
但是,如果我尝试两次调用“通知”方法,则第二封电子邮件为空白,我知道这是因为对象缓冲区,但我想不出任何其他方法来做到这一点。
谢谢,
伊恩
I am trying to figure out the best way to send emails from an external template file, at the moment I have a template file that looks like this:
Thank you, your order has been received, someone will review it and process it. No money has been taken from your account.
<?php
echo date('Y-m-d H:i:s');
?>
<pre>
<?php print_r($this->data); ?>
</pre>
And then my send method looks like this:
public function notify($template) {
// get the template from email folder
$path = $_SERVER['DOCUMENT_ROOT'].'templates/email/'.$template.'.php';
if(file_exists($path)) {
ob_start();
require_once($path);
$body = ob_get_contents();
ob_end_clean();
$subject = 'email send';
foreach($this->emailTo as $email)
new Mail($email,$subject,$body);
}
}
This all works fine when I call it like this:
$notifications = new notifications();
$notifications->setData(array('order' => $order->order));
$notifications->addEmail($order->order->email);
$notifications->notify('orderReceived');
However, if I try to make two calls to the "notify" method then the second email is blank, I know this is because the object buffer, but I cannot think of any other way to do it.
Thanks,
Ian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用
require_once
,因此文件只会加载一次。尝试require
。还可以考虑加载纯文本模板并使用 str_replace 替换模板中的变量,如下所示:
You are using
require_once
, so the file will only load once. Tryrequire
.Also consider loading a pure text template and use str_replace to replace the variables in the template like this:
我会这样做:
模板文件
通知功能
这将解决问题 - 无论如何都可以通过将
require_once
更改为require< /代码>。
使用
require_once
意味着模板文件只会被加载一次(线索在函数名称中),因此第二次调用将导致空白正文。I would do this:
Template file
Notify function
This will solve the problem - which could be solved anyway by changing
require_once
torequire
.Using
require_once
means the template file will only be loaded once (clue's in the function name), so the second call will result in a blank body.