I'm trying to add some variables to email's template to be rendered before email is sent.

I was able to modify emailModel like customHeaders and subject but I couldn't pass custom variables to the template.

How can I pass variables to email's template using onBeforeSendEmail event?

2 Answers 2


The onBeforeSendEmail event passes in 3 parameters.

$event->params['user'] which is a UserModel object representing the user that is receiving the email.

$event->params['emailModel'] which is the EmailModel defining the email to be sent.

$event->params['variables'] which is an associative array of key => value pairs of that is sent to the template.

You can modify the 3rd one to pass in any additional variables that you need like so from your plugin's main class file:

public function init()

    craft()->on('email.onBeforeSendEmail', function ($event)
        $event->params['variables']['newKey'] = 'newValue';


There is a bug in the latest build of Craft 2.5.2754 that prevents this from working properly, but it's been fixed for the next release.

  • If you don't mind, can you show me how can I modify variables and pass additional ones to it?
    – mohd4482
    Dec 12, 2015 at 10:43
  • Edited my answer.
    – Brad Bell
    Dec 14, 2015 at 23:21
  • It works on Craft 2.5.2755
    – mohd4482
    Dec 18, 2015 at 16:06

I don't know if this is the same kind of thing you are looking for, but I think it's close.

I created a plugin that handles a contact form submission. When the user fills out the form, an email is sent to me & the person who filled out the form. I'm not listening to onBeforeSendEmail, but rather listening for a save event that I call via controller.

I told Craft that I needed a settings area in my plugin, then I created a textarea where I could manage the email that was sent to the user(s). This is what that looks like:

<p>Hi {{firstName}} &mdash; thank you for reaching out to us!<br>We wanted to drop you a quick note that we've received your information and we'll be in touch very soon!</p><p>Regards,<br>The friendly folks at this website</p>

To get {{firstName}} to use the value that were entered in the form, I used this in my plugin's service file.

That way it would read "Hi Steve - thank you for..."


$email = new EmailModel();
$email->fromEmail = $emailSettings['emailAddress']; // set in CP
$email->replyTo = $emailSettings['emailAddress'];   // set in CP
$email->sender = $emailSettings['emailAddress'];    // set in CP
$email->fromName = $emailSettings['senderName'];    // set in CP
$email->toEmail = $contact->email;
$email->subject = $settings->guestSubject;
$email->body = craft()->templates->renderString($settings->welcomeEmailMessage, array('firstName' => $contact->firstName));

if (craft()->email->sendEmail($email)) {
    return true;

It's the craft()->templates->renderString() part that lets me hook into my plugin to set the variable values.

Hope this helps!

  • Thanks! but it didn't solve my problem.
    – mohd4482
    Dec 11, 2015 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.