1

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 = '[email protected]';

    $mailToBeSent->toEmail = '[email protected]';

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

    $mailer->sendEmail($mailToBeSent);
}

Folder structure:

Folder structure

3
  • 1
    Try adding a craft()->templates->setTemplateMode(TemplateMode::Site); line before the call to craft()->templates->render().
    – Brad Bell
    Commented Sep 14, 2017 at 15:12
  • 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
    Commented Sep 15, 2017 at 5:56
  • No problem and done!
    – Brad Bell
    Commented Sep 15, 2017 at 18:42

1 Answer 1

1

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 = '[email protected]';

    $mailToBeSent->toEmail = '[email protected]';

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

Your Answer

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

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