Sending localised emails from a Craft CMS plugin

I've been using the Craft content management system (CMS) for over a year now. It is by far the best CMS I have ever used from both perspectives of developer and end user.

The sheer flexibility of the system has meant that I have achieved what I've wanted with every site I've built with it using mostly built in functionality, only using their own contact form plugin as an extra. However, on a recent job I needed to customise the built in user registration process so I took this as an opportunity to dip my toes into Craft plugin development waters.

As with working with any new system there was a bit of a learning curve but I soon got my head around it and in a matter of hours had the first revision of the plugin up and running. One of the tasks required of the plugin was to send a customised email to an administrator after someone had registered and verified their email address. After a bit of digging around in various places online and a bit of trial and error I got this working, so I thought I'd collate this information and share how I did it.

Craft has built in functionality for sending emails; sendEmail() and sendEmailByKey(). I chose the latter function which sends emails by a given key. Craft comes with a few built in email keys such as “account_activation" but plugins can register their own keys so I added the following to my main plugin class (you can find more general information on plugin development on the Craft website):

function registerEmailMessages()
{
	return array(
	        'user_authorisation'
	);
}

I then added a folder named “translations" within my plugin folder and added a file named “en_gb.php" within it (you can replace en_gb with the locale/s of your choice) and added the following lines (some lines in “user_authorisation_body" omitted for brevity):

<?php

return array (
	'user_authorisation_heading' => 'When someone has verified their email address:',
	'user_authorisation_subject' => 'Authorise user account',
	'user_authorisation_body' => "Hi,\n\n" .
		"The following user has created an account on {{siteName}} and verified their email address.\n\n" .
		"Name: {{registeredUser.customerFirstName}} {{registeredUser.customerLastName}}\n\n" .
		"Username: {{registeredUser.friendlyName}}\n\n" .
		"Email: {{registeredUser.email}}\n\n" .
		"Telephone: {{registeredUser.telephone}}\n\n" .
		"Mobile: {{registeredUser.mobile}}\n\n"
);

I was then able to send the email using the following code to send the email to the administrator passing in the user as a parameter in order to populate the email body.

craft()->email->sendEmailByKey($adminUser, 'user_authorisation', array(
	'registeredUser' => $user
));

So with relatively few lines of code I was able to send a translatable email with customer details from within a plugin. It's easy to see how this could be extended to send further information such as a customer's order information in an ecommerce application and demonstrates the power and flexibility built in to Craft. This process also had the added bonus of having the email template show up in the messages tabs in the Email settings section so it could be edited in the Pro version of Craft.

My first taste of Craft plugin development has left me hungry for more so I'm really looking forward to furthering my experience over the course of the year.


comments powered by Disqus