Using Localizable E-mail Templates
Wouldn't it be great to be able to standardize how mails sent from your Community Server site looks, and to be able to change them on demand without having to re-compile anything?
As you might know Community Server (CS) already has a built-in system for this. This localizable template system is used for example when a new user registers and an e-mail with login information is sent to that user. In this article I want to show you how you can hook yourself into and use this system in your own Community Server modules. Also how you can add your own e-mail template for a certain process.
Starting with this article I will also try to always have any code samples in both C# and VB (since I am currently trying to learn C# to see what all the fuss is about
). But, for all the VB readers of this blog, don't worry, this is not a step towards having only C# samples in this blog in the future.
I have touched on the subject before in an earlier article regarding Notifications for moderated posts to forums. But I thought it was worth to lift out the E-mail template function again, and discuss it separately.
The E-mail templates
First let's take a look at the e-mail templates and where you'll find them. CS has a Languages folder in the web root, which holds a subfolder for each language. Within each language folder there's a couple of .XML files which stores different localized resources that CS uses.

Click on the picture to see it in original size
The .XML file that holds the e-mail templates is found under /Languages/<localized language>/emails, in the file called emails.xml. Out-of-the-box CS will have one localized language folder of en-US.

Click on the picture to see it in original size
As you can see in the picture above, each e-mail template is contained within an email node. The email node can contain child nodes for i.e. subject, from and body. Also you can see that the text contains some place holders here and there (i.e. [ ModerateURL ])
The e-mail template is identified by it's emailType, which we will see later on in the code sample. The e-mail template seen in the picture above is of the emailType 'ModeratorEmailNotification'.
Using the E-mail templates in your code
To use these e-mail templates in your code you need to:
- Get the template into a MailMessage object
- Fill in the placeholders with the desired values
- Send the MailMessage or put it on CS's E-mail queue
The following code samples does number 1 and 2 of the above. You will need references to the following namespaces for your code to work.
System.Text
System.Web
CommunityServer.Components
Visual Basic

Click on the picture to see it in original size
C#

Click on the picture to see it in original size
First an Instance of the EmailTemplateProvider is used to get the desired e-mail template and put it into a MailMessage object. One of the arguments of the GetTemplate method is the user object for the user we want to send the mail to.
For certain built-in e-mail templates there are populate methods that uses i.e. this user object to fill in information in the placeholder. Although for the e-mail template I am using in the samples here, I could not find any built-in populate methods that worked. Therefore I've used another way to fill in these placeholder with the data I want.
On the body and subject fields, I've used Regex.Replace to replace their placeholders. To get the admin e-mail address, to send the mail from, I've used the CSContext.Current.SiteSettings object.
To send the populated mail to a group of user or just one user, you could use the following samples.
Visual Basic

Click on the picture to see it in original size
C#

Click on the picture to see it in original size
This method takes the arguments of a UserSet of the users you want to send the e-mails to, and a ForumPost object that (in this case) I want to refer to in my notification e-mail. Of course in your situation if applying these samples to your own module, you might not need these arguments at all, or just the UserSet, depending on what you want to do. The important thing with this sample is to show how you can put an MailMessage on the CS E-mail Queue with an Instance of the EmailQueueProvider object.
Summary
In this article I have showed you how the built-in e-mail templates in CS are structured and where you can find them. I also showed how you can programmatically fetch one of these e-mail templates and populate it's placeholders. Lastly a code sample showing you how to add the e-mail to the CS E-mail Queue, so that CS sends it the next time the CS E-mail job is run.
With this information you should be able to use the built-in e-mail templates in your code, and to create your own localizable e-mail templates for your CS modules. So let's go and try it out already!
If you enjoyed this post Subscribe to my feed via RSS or e-mail!