使用模板文件发送电子邮件

发布于 2024-12-19 02:27:08 字数 1128 浏览 0 评论 0原文

我正在尝试找出从外部模板文件发送电子邮件的最佳方式,目前我有一个如下所示的模板文件:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分开我的手 2024-12-26 02:27:08

您正在使用 require_once,因此文件只会加载一次。尝试 require

还可以考虑加载纯文本模板并使用 str_replace 替换模板中的变量,如下所示:

$template = "<pre>%DATA%</pre>";
$text = str_replace('%DATA%', $this->data, $template);

You are using require_once, so the file will only load once. Try require.

Also consider loading a pure text template and use str_replace to replace the variables in the template like this:

$template = "<pre>%DATA%</pre>";
$text = str_replace('%DATA%', $this->data, $template);
温柔嚣张 2024-12-26 02:27:08

我会这样做:

模板文件

Thank you, your order has been received, someone will review it and process it. No money has been taken from your account.

%s

<pre>
%s
</pre>

通知功能

public function notify($template) {
    // get the template from email folder
    $path = $_SERVER['DOCUMENT_ROOT'].'templates/email/'.$template.'.php';
    if (!file_exists($path)) {
        // Return false if the template is missing
        return FALSE;
    }
    // Create the message body and subject
    $body = sprintf(file_get_contents($path), date('Y-m-d H:i:s'), print_r($this->data, TRUE));
    $subject = 'email send';
    // Send the mail(s)
    foreach($this->emailTo as $email) {
        new Mail($email, $subject, $body);
    }
    // Return true for success
    return TRUE;
}

这将解决问题 - 无论如何都可以通过将 require_once 更改为 require< /代码>。

使用 require_once 意味着模板文件只会被加载一次(线索在函数名称中),因此第二次调用将导致空白正文。

I would do this:

Template file

Thank you, your order has been received, someone will review it and process it. No money has been taken from your account.

%s

<pre>
%s
</pre>

Notify function

public function notify($template) {
    // get the template from email folder
    $path = $_SERVER['DOCUMENT_ROOT'].'templates/email/'.$template.'.php';
    if (!file_exists($path)) {
        // Return false if the template is missing
        return FALSE;
    }
    // Create the message body and subject
    $body = sprintf(file_get_contents($path), date('Y-m-d H:i:s'), print_r($this->data, TRUE));
    $subject = 'email send';
    // Send the mail(s)
    foreach($this->emailTo as $email) {
        new Mail($email, $subject, $body);
    }
    // Return true for success
    return TRUE;
}

This will solve the problem - which could be solved anyway by changing require_once to require.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文