覆盖 cakePHP3 电子邮件的内容

override the content of cakePHP3 emails

我构建了一个 cakePHP3 应用程序,我正在寻找有关以下情况的一些建议。电子邮件配置设置如下,每当创建新订单时发送邮件。

public function orderCreated($order, array $attachments = [])
    {
        $email = $this
            ->locale($order->customer->language)
            ->to($order->customer->email)
            ->from(Configure::read('Webshop.email'), Configure::read('Webshop.name'))
            ->subject(__('Confirmation of your order {0}', $order->nr))
            ->template('orders' . DS . 'order_created')
            ->set(['order' => $order])
            ->attachments($attachments);

        return $email;
    }

多年来一直运行良好,但我想为这个特定的电子邮件(和其他电子邮件)添加一些新功能。如果管理员愿意,他们应该能够覆盖 orders/order_created.ctp 模板的内容,这样他们就可以自己定义这封电子邮件的内容。因此他们不必依赖我提供的 order_created.ctp 模板中的相同内容。

创建一个 UI 来保存他们自己的电子邮件不是问题。但是我真的不知道如何将覆盖的内容提供给 cakePHP3 邮件程序。我试过设置

 ->template(false)
 ->message($new_content)

但这并没有帮助。邮件未到达邮箱,因为正文为空。

谢谢。

我想我会选择这样的东西:

->template('orders' . DS . 'order_created_custom')
->set(['order' => $order, 'content' => $new_content])

然后在新的 order_created_custom.ctp 中你会输出 $content。这使您可以选择根据 $order 对内容进行一些文本替换,以及可能具有标准称呼或签名等内容。