2

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?

| improve this question | | | | |
1

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()
{
    parent::init();

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

Note:

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.

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

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..."

MyPluginService.php

...
$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!

| improve this answer | | | | |
  • Thanks! but it didn't solve my problem. – dralshehri Dec 11 '15 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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