Developers

Emails

Sending an email manually

$variables = [
    'user' => 'John'
];

Craft::$app->getMailer()
    ->composeFromKey('email_key', $variables)
    ->setTo('[email protected]')
    ->send();

The email will automatically be modified according to its config before it's sent.

Modify email variables

You may modify variables passed to any email using this event :

Event::on(
    Mailer::class, 
    Mailer::EVENT_BEFORE_PREP, 
    function (MailEvent $e) {
        $e->message->variables['name'] = 'value';
    }
);

Emails shots

Define email sources

Create a new class for the source :

<?php

use Ryssbowh\CraftEmails\interfaces\EmailSourceInterface;
use craft\base\Component;

class MyEmailSource extends Component implements EmailSourceInterface
{
    /**
     * @inheritDoc
     */
    public function getName(): string
    {
        return 'My source';
    }

    /**
     * @inheritDoc
     */
    public function getHandle(): string
    {
        return 'my_source';
    }

    /**
     * @inheritDoc
     */
    public function getEmails(): array
    {
        return [
            '[email protected]' => 'Hello'
        ];
    }
}

And register it on the event :

Event::on(
    EmailSourceService::class,
    EmailSourceService::EVENT_REGISTER,
    function (RegisterEmailSourcesEvent $e) {
        $e->add(new MyEmailSource);
    }
);

Email sources must implement EmailSourceInterface. Exceptions will be thrown when registering sources which handles are already defined.

Variables

You can define variables manually before the shot is sent, they will be passed to the email :

Event::on(
    EmailShotsService::class,
    EmailShotsService::EVENT_BEFORE_SEND,
    function (SendEmailShotEvent $e) {
        $e->shot->variables = [
            'var' => 'value'
        ];
    }
);

For global variables, refer to the Craft documentation

You must enable javascript to view this website