Skip to main content

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);
}
)

Prepare Notification Before SendingNew in 5.10.8+

This event allows you to to modify a notification email just before it's sent. Here is an example of how to modify a specific template, in this case a User Select notification template, based on a posted value:

use Solspace\Freeform\Events\Notifications\PrepareNotificationEvent;
use Solspace\Freeform\Notifications\Components\Recipients\RecipientCollection;
use Solspace\Freeform\Notifications\Types\Dynamic\Dynamic;
use yii\base\Event;

Event::on(
Dynamic::class,
Dynamic::PREPARE_NOTIFICATION,
function (PrepareNotificationEvent $event) {
$form = $event->getForm();
$notification = $event->getNotification();

// Check if the notification is the one we want to modify
if ($notification->getName() !== 'My User Select Notification') {
return;
}

// Get the posted value for the associated field
$value = $form->get('dropdown')->getValue();

// Determine what actual recipients we want to use based on the posted value
$recipients = [];
switch ($value) {
case 'one':
$recipients[] = 'first@recipient.com';
break;

case 'two':
$recipients[] = 'second@recipient.com';
break;
}

// Create a new RecipientCollection and set it on the event
$collection = RecipientCollection::fromArray($recipients);
$event->setRecipients($collection);
}
);

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']);
}
)

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
}
)