OctoberCMS 创建自定义邮件布局/邮件部分

OctoberCMS Create Custom Mail Layout / Mail Partial

我正在使用 October 的 Mail 服务,看起来不错。但是,如果可以使用插件甚至主题创建新的 Mail Layout,我看不到文档中提到它,我相信将布局存储在数据库中并不是最好的方法,特别是在开发时带有邮件布局的自定义主题,或带有邮件布局的插件(这是我的情况)。

有什么方法可以通过代码从头开始创建自定义 Mail LayoutsMail Partials(无需使用后端)?

注意:我非常了解邮件模板和邮件(布局/部分)之间的区别,它通过后端与我一起工作,我只需要一种方法将它们与我的插件捆绑在一起。

您可以使用插件迁移文件注册邮件布局,因为目前无法直接从插件配置文件注册它们。

如何插入布局 参考:https://github.com/octobercms/october/issues/2000

you can do same for the mail partial. its model is System\Models\MailPartial

\System\Models\MailPartial::create([
    'is_custom'    => true,
    'name'         => 'Default',
    'code'         => 'default',
    'content_html' => $html,
    'content_text' => $text,
]);

对于templates/views,您可以将邮件模板放在插件视图目录内的mail 目录中。

参考:https://octobercms.com/docs/services/mail#mail-views

在邮件中您可以定义您使用迁移文件手动插入的布局名称。并将其用作布局。

registering mail templates inside plugin.php config file.

public function registerMailTemplates()
{
    return [
        'rainlab.user::mail.activate' => 'Activation mail sent to new users.',
        'rainlab.user::mail.restore'  => 'Password reset instructions for front-end users.'
    ];
}

'rainlab.user::mail.activate' => {plugin-name}::{mail.<template-name>} then provide your subject.

现在安装插件时,

  1. 迁移文件会将您的布局插入数据库
  2. 插件注册将注册您在registerMailTemplates方法
  3. 中定义的视图
  4. 现在,当您使用此模板时,他们将使用您的 Layouts,因为您已经在模板中设置了 "layout='your-layout-code'"

您所有与布局和模板相关的内容现在都在您的模板中,当您安装插件时,它也会注册您的布局和模板。

如有任何疑问,请发表评论。