Mailing Events

You can extend the Mailer service functionality to manipulate emails that are sent out to users after form submissions:

Before email values are parsed with Twig

This event is called before all notification values are parsed with Twig. Use this event to add your own values or manipulate the existing ones.

use Solspace\Freeform\Services\MailerService;
use Solspace\Freeform\Events\Mailer\RenderEmailEvent;

Event::on(
  MailerService::class,
  MailerService::EVENT_BEFORE_RENDER,
  function (RenderEmailEvent $event) {
    $form = $event->getForm();
    $notification = $event->getNotification();
    $fieldValues = $event->getFieldValues();
    $submission = $event->getSubmission();

    // Attach a custom value to the field values array
    // thus getting access to "{{ myCustomValue }}" in your notification twig template
    $fieldValues['myCustomValue'] = 'test';

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

Before an email is sent

Use this event to change the emails that are sent out, or perform any other tasks that need to be done before emails are sent out.

use Solspace\Freeform\Services\MailerService;
use Solspace\Freeform\Events\Mailer\SendEmailEvent;

Event::on(
  MailerService::class,
  MailerService::EVENT_BEFORE_SEND,
  function (SendEmailEvent $event) {
    $message = $event->getMessage();
    $form = $event->getForm();
    $notification = $event->getNotification();
    $fieldValues = $event->getFieldValues();
    $submission = $event->getSubmission();

    // Manipulate the message object to send to a Bcc recipient
    $message->setBcc(['stealthy@email.com']);
  }
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

After an email is sent

This event is fired right after an email is sent out. You might use this event to log the sent out email somewhere.

use Solspace\Freeform\Services\MailerService;
use Solspace\Freeform\Events\Mailer\SendEmailEvent;

Event::on(
  MailerService::class,
  MailerService::EVENT_AFTER_SEND,
  function (SendEmailEvent $event) {
    $message = $event->getMessage();
    $form = $event->getForm();
    $notification = $event->getNotification();
    $fieldValues = $event->getFieldValues();
    $submission = $event->getSubmission();

    // Perform some action here
  }
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Last Updated: 7/24/2019, 10:56:44 AM