<p>Changing <code>sendNotificationEmail()</code> to this should fix it for you:</p>

<pre><code>public function sendNotificationMail(UserModel $user) {
    $mailer = craft()-&gt;email;
    $mailToBeSent = new EmailModel();

    $mailToBeSent-&gt;subject = Craft::t('Nieuwe gebruiker geregistreerd');
    $mailToBeSent-&gt;fromName = 'xyz';
    $mailToBeSent-&gt;sender = 'noreply@xyz.be';

    $mailToBeSent-&gt;toEmail = 'xyz@xyz.be';

    $originalTemplateMode = craft()-&gt;templates-&gt;getTemplateMode();
    craft()-&gt;template-&gt;setTemplateMode(TemplateMode::Site);

    $content = craft()-&gt;templates-&gt;render(
        '_emails/customer-service-notification',
        [
            'userInfo' =&gt; [
                'email' =&gt; $user-&gt;email,
                'name' =&gt; $user-&gt;fullName,
            ],
        ]
    );

    $mailToBeSent-&gt;htmlBody = $content;
    $mailer-&gt;sendEmail($mailToBeSent);

    craft()-&gt;templates-&gt;setTemplateMode($originalTemplateMode);
}
</code></pre>

CC BY-SA 3.0
2017-09-15T18:42:24.657Z
2017-09-15T18:42:24.657Z
Brad Bell
<p><strong>Background:</strong></p>

<p>I'm working on a site that has frontend registration with some custom fields. The client would like to have a notification email whenever someone activates their account, containing the info on that user (so name, email and the custom fields). </p>

<hr>

<p><strong>Issue at hand:</strong></p>

<p>For some reason, I'm getting the following error when I try to send an HTML email on the 'onActivateUser': </p>

<blockquote>
  <p>Unable to find the template “_emails/customer-service-notification”.</p>
</blockquote>

<p>If however I try to send a plain text email, it all goes without problem. </p>

<p>I'd prefer to send out an HTML email so I'd like to resolve this. </p>

<hr>

<p><strong>What I already tried:</strong></p>

<ul>
<li>Triggering the service function in another way (via custom controller action) sends the html email just fine</li>
<li>Sending the mail by just setting the content as string in the function works just fine</li>
</ul>

<hr>

<p><strong>Code</strong>:</p>

<p><em>Main plugin file - init function</em></p>

<pre><code>craft()-&gt;on('users.onActivateUser', function (Event $event){
    $user = $event-&gt;params['user'];
    craft()-&gt;plugin_customerServiceNotification-&gt;sendNotificationMail($user);
});
</code></pre>

<p><em>customerServiceNotification</em></p>

<pre><code>public function sendNotificationMail(UserModel $user){
    $mailer = craft()-&gt;email;
    $mailToBeSent = new EmailModel();

    $mailToBeSent-&gt;subject = Craft::t('Nieuwe gebruiker geregistreerd');
    $mailToBeSent-&gt;fromName = 'xyz';
    $mailToBeSent-&gt;sender = 'noreply@xyz.be';

    $mailToBeSent-&gt;toEmail = 'xyz@xyz.be';

    $content = craft()-&gt;templates-&gt;render(
        '_emails/customer-service-notification',
        [
            'userInfo' =&gt; [
                'email' =&gt; $user-&gt;email,
                'name' =&gt; $user-&gt;fullName,
            ],
        ]
    );
    $mailToBeSent-&gt;htmlBody = $content;

    $mailer-&gt;sendEmail($mailToBeSent);
}
</code></pre>

<p>Folder structure:</p>

<p><a href="https://i.sstatic.net/qei8H.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/qei8H.png" alt="Folder structure"></a></p>

CC BY-SA 3.0
2017-09-14T15:12:29.767Z
Try adding a `craft()->templates->setTemplateMode(TemplateMode::Site);` line before the call to `craft()->templates->render()`.
Brad Bell
CC BY-SA 3.0
2017-09-15T05:56:45.223Z
That seems to have done the trick, thanks a lot! If you want to add it as answer, than I'll make sure to accept it as well :)
mrsmn
CC BY-SA 3.0
2017-09-15T18:42:39.977Z
No problem and done!
Brad Bell
<plugin-development><emails><events>
Error when trying to send HTML email on event onActivateUser
# Error when trying to send HTML email on event onActivateUser

(Asked by: username_0 on 2017-09-14)

Background:
I'm working on a site that has frontend registration with some custom fields. The client would like to have a notification email whenever someone activates their account, containing the info on that user (so name, email and the custom fields). 

Issue at hand:
For some reason, I'm getting the following error when I try to send an HTML email on the 'onActivateUser': 

Unable to find the template “_emails/customer-service-notification”.

If however I try to send a plain text email, it all goes without problem. 
I'd prefer to send out an HTML email so I'd like to resolve this. 

What I already tried:

Triggering the service function in another way (via custom controller action) sends the html email just fine
Sending the mail by just setting the content as string in the function works just fine


Code:
Main plugin file - init function
craft()->on('users.onActivateUser', function (Event $event){
    $user = $event->params['user'];
    craft()->plugin_customerServiceNotification->sendNotificationMail($user);
});

customerServiceNotification
public function sendNotificationMail(UserModel $user){
    $mailer = craft()->email;
    $mailToBeSent = new EmailModel();

    $mailToBeSent->subject = Craft::t('Nieuwe gebruiker geregistreerd');
    $mailToBeSent->fromName = 'xyz';
    $mailToBeSent->sender = 'noreply@xyz.be';

    $mailToBeSent->toEmail = 'xyz@xyz.be';

    $content = craft()->templates->render(
        '_emails/customer-service-notification',
        [
            'userInfo' => [
                'email' => $user->email,
                'name' => $user->fullName,
            ],
        ]
    );
    $mailToBeSent->htmlBody = $content;

    $mailer->sendEmail($mailToBeSent);
}

Folder structure:

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

> **Comments:**
> **username_1**: Try adding a `craft()->templates->setTemplateMode(TemplateMode::Site);` line before the call to `craft()->templates->render()`.  
> **username_0**: That seems to have done the trick, thanks a lot! If you want to add it as answer, than I'll make sure to accept it as well :)  
> **username_1**: No problem and done!  

---

## 1 answers

### Answer by username_1 on 2017-09-15. Score: 1 | Accepted answer

Changing sendNotificationEmail() to this should fix it for you:
public function sendNotificationMail(UserModel $user) {
    $mailer = craft()->email;
    $mailToBeSent = new EmailModel();

    $mailToBeSent->subject = Craft::t('Nieuwe gebruiker geregistreerd');
    $mailToBeSent->fromName = 'xyz';
    $mailToBeSent->sender = 'noreply@xyz.be';

    $mailToBeSent->toEmail = 'xyz@xyz.be';

    $originalTemplateMode = craft()->templates->getTemplateMode();
    craft()->template->setTemplateMode(TemplateMode::Site);

    $content = craft()->templates->render(
        '_emails/customer-service-notification',
        [
            'userInfo' => [
                'email' => $user->email,
                'name' => $user->fullName,
            ],
        ]
    );

    $mailToBeSent->htmlBody = $content;
    $mailer->sendEmail($mailToBeSent);

    craft()->templates->setTemplateMode($originalTemplateMode);
}


---

