Email Notification Events

If you wish to extend the capabilities of Email Notifications in Express Forms, feel free to use any of the events listed below:

Before Rendering Email Notification Values

The EVENT_BEFORE_RENDER event is called when the template variables and field values are all collected, letting you do any additional modifications before each of the email properties gets rendered using the collected values. This event is useful if you wish to add additional template variables to your email notifications.

use Solspace\ExpressForms\services\EmailNotifications;
use Solspace\ExpressForms\events\emailNotifications\RenderEmailValuesEvent;

Event::on(
    EmailNotifications::class,
    EmailNotifications::EVENT_BEFORE_RENDER,
    function (RenderEmailValuesEvent $event) {
        if ($event->getNotification()->getFileName() !== "specific_template.twig") {
            return;
        }

        $templateVariables = $event->getTemplateVariables();
        $templateVariables['additionalVariable'] = 'Has a value now';

        $event->setTemplateVariables($templateVariables);
    }
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Before Sending the Email

The EVENT_BEFORE_SEND event gets triggered right before the Craft mailer sends out the message.

TIP

This event is cancelable, so you can prevent it from sending.

use Solspace\ExpressForms\services\EmailNotifications;
use Solspace\ExpressForms\events\emailNotifications\SendEmailEvent;

Event::on(
    EmailNotifications::class,
    EmailNotifications::EVENT_BEFORE_SEND,
    function (SendEmailEvent $event) {
        // Attach a ninja BCC to all emails
        $email = $event->getEmail();
        $email->setBcc('[email protected]');
    }
);
1
2
3
4
5
6
7
8
9
10
11
12

After Sending the Email

The EVENT_AFTER_SEND event gets triggered right after the Craft mailer sends out the message.

use Solspace\ExpressForms\services\EmailNotifications;
use Solspace\ExpressForms\events\emailNotifications\SendEmailEvent;

Event::on(
    EmailNotifications::class,
    EmailNotifications::EVENT_AFTER_SEND,
    function (SendEmailEvent $event) {
        // Perform some logging, probably, idk...
    }
);
1
2
3
4
5
6
7
8
9
10
Last Updated: 4/23/2019, 10:35:37 AM