I'm writing a simple project-specific plugin that sends a notification to the site admin when a new user is registered. I have an HTML template that I'd like to use for this.
In my plugin's main class, I have the following function:
public function init()
{
parent::init();
// Events
craft()->on('users.saveUser', function(Event $event) {
$user = $event->params['user'];
$isNewUser = $event->params['isNewUser'];
if ($isNewUser)
{
// Send notification email
craft()->aeiMember->registrationNotification($user);
}
});
}
which calls a one of my plugin's services, 'registrationNotification'. Here's the code I have for that service:
public function registrationNotification(UserModel $savedUser)
{
$user = new \stdClass();
$user->firstName = $savedUser->firstName;
$user->lastName = $savedUser->lastName;
$user->email = $savedUser->email;
$user->aeiGroups = $savedUser->aeiGroups;
$user->phone = $savedUser->phone;
$user->mailingAddress = $savedUser->mailingAddress;
$user->city = $savedUser->city;
$user->state = $savedUser->state;
$user->zip = $savedUser->zip;
$user->country = $savedUser->country;
$user->preferredDealer = $savedUser->preferredDealer;
$user->dealerAccountNumber = $savedUser->dealerAccountNumber;
$user->school = $savedUser->school;
$user->expectedGraduationDate = $savedUser->expectedGraduationDate;
$email = new EmailModel();
$email->toEmail = '...myemailaddress...';
$email->subject = 'New User Registration: ' . $user->firstName . ' ' . $user->lastName;
$email->htmlBody = craft()->templates->render('aeiMember/registration_notification', array(
'user' => $user,
));
// Send
if (craft()->email->sendEmail($email))
{
return true;
}
}
When I submit the registration form, I get the following error: "CDbException: CDbTransaction is inactive and cannot perform commit or roll back operations.". I narrowed down the issue, and if I use a non-HTML email body, everything works fine, so apparently there's an issue with rendering a template at this event.
Here's a link to a Google Doc with the stack trace: https://docs.google.com/document/d/1atFNkktuZQ91hU9QipXYMhOCHNGAavO5qUI_TF5jSeE/edit?usp=sharing
Am I doing something wrong, or is there a workaround to accomplish what I'm after? It seems like it should be pretty simple.