<p>The <a href="https://craftcms.com/docs/plugins/events-reference#email-onBeforeSendEmail" rel="nofollow">onBeforeSendEmail</a> event passes in 3 parameters.</p>

<p><code>$event-&gt;params['user']</code> which is a <a href="http://buildwithcraft.com/classreference/models/UserModel" rel="nofollow">UserModel</a> object representing the user that is receiving the email.</p>

<p><code>$event-&gt;params['emailModel']</code> which is the <a href="http://buildwithcraft.com/classreference/models/EmailModel" rel="nofollow">EmailModel</a> defining the email to be sent.</p>

<p><code>$event-&gt;params['variables']</code> which is an associative array of key => value pairs of that is sent to the template.</p>

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

<pre><code>public function init()
{
    parent::init();

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

<p><strong>Note:</strong></p>

<p>There is a bug in the latest build of <a href="https://craftcms.com/changelog#build2754" rel="nofollow">Craft 2.5.2754</a> that prevents this from working properly, but it's been fixed for the next release.</p>

CC BY-SA 3.0
2015-12-12T10:43:41.490Z
If you don't mind, can you show me how can I modify variables and pass additional ones to it?
mohd4482
CC BY-SA 3.0
2015-12-14T23:21:40.390Z
Edited my answer.
Brad Bell
CC BY-SA 3.0
2015-12-18T16:06:29.093Z
It works on Craft 2.5.2755
mohd4482
CC BY-SA 3.0
2015-12-11T19:22:25.147Z
2015-12-14T23:21:26.353Z
2015-12-14T23:21:26.353Z
Brad Bell
<p>I don't know if this is the same kind of thing you are looking for, but I think it's close.</p>

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

<p>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>

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

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

<p>That way it would read "Hi Steve - thank you for..."</p>

<p><strong>MyPluginService.php</strong></p>

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

if (craft()-&gt;email-&gt;sendEmail($email)) {
    return true;
}
....
</code></pre>

<p>It's the <code>craft()-&gt;templates-&gt;renderString()</code> part that lets me hook into my plugin to set the variable values.</p>

<p>Hope this helps!</p>

CC BY-SA 3.0
2015-12-11T19:03:44.367Z
Thanks! but it didn't solve my problem.
mohd4482
CC BY-SA 3.0
2015-12-11T14:46:16.420Z
2015-12-11T14:46:16.420Z
Damon
<p>I'm trying to add some variables to email's template to be rendered before email is sent.</p>

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

<p>How can I pass variables to email's template using onBeforeSendEmail event?</p>

<plugin-development><templating><emails><events>
How can I pass variables to email's template onBeforeSendEmail
# How can I pass variables to email's template onBeforeSendEmail

(Asked by: username_0 on 2015-12-11)

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?

**Tags:** plugin-development, templating, emails, events


---

## 2 answers

### Answer by username_1 on 2015-12-11. Score: 1 | Accepted answer

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.

> **Comments:**
> **username_0**: If you don't mind, can you show me how can I modify variables and pass additional ones to it?  
> **username_1**: Edited my answer.  
> **username_0**: It works on Craft 2.5.2755  

---

### Answer by username_2 on 2015-12-11. Score: 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:
Hi {{firstName}} — thank you for reaching out to us!We wanted to drop you a quick note that we've received your information and we'll be in touch very soon!Regards,The friendly folks at this website

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!

> **Comments:**
> **username_0**: Thanks! but it didn't solve my problem.  

---

