Ad

OctoberCMS Create Custom Mail Layout / Mail Partial

- 1 answer

I'm using October's Mail service, it looks nice. However, I can't see it is mentioned in the documentation if it's possible to create new Mail Layout with plugins or even themes, I believe storing the layouts in the database is not the best way to do it, specially when developing a custom theme with mail layout, or a plugin with mail layout (which is my case).

Is there any way to create a custom Mail Layouts and Mail Partials from ground up by code (without the need to use the backend) ?

Note: I'm really aware about the difference between Mail Templates and Mail (Layouts / Partials), it works with me through the backend, I only need a way to bundle them with my plugin.

Ad

Answer

You can register mail layouts using plugins migration files as for now there is no way to register them directly from plugin config file.

how you can insert layouts reference : 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,
]);

and for templates/views you can put your mail templates in mail directory inside plugin's view directory.

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

here within the mail you can defined your layout name which you have inserted manually using migration file. and use it as layout.

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.

now when you install plugin,

  1. migration file will insert your Layout in to database
  2. plugin registration will register views which you defined in registerMailTemplates method
  3. now when you use this template they will use your Layouts as you have already set "layout='your-layout-code'" in templates.

Your all Layout and template related is now inside your template and when you install plugin it will register your layout and template as well.

if any query please comment.

Ad
source: stackoverflow.com
Ad