I needed to create an Email Alert function the other day to send emails to a designated Community Server Role when a certain background task encountered an error. Posting an event to the CS Event log wasn't good enough for us this time. In the process of assemblying the custom email function I thought it a good example of the basic ingredients that comprise the Community Server email process.
First we're going to create a new Email Alert template in the \languages\en-US\emails\emails.xml template file. We've specified both TEXT and HTML versions of the message and will need to populate our tokens, "ErrorType", "InternalExceptionUrl", "ExternalExceptionUrl", etc. The email will tell us an error has occurred and provide direct links to the site Exception Log. There's also a "DistributionList" token (in the HTML template) which I like to include in my custom email processes because I think it's useful for each recipient to know who all is receiving the message.
The background task creates an instance of a custom DBVTEmailsProvider and gets the ball rolling with a populated MailPost object and the role to whom we want to send the email.
The Email Provider populates the tokens for each user in the role and places the email message in the Email Queue. Highlighted is the process of populating the tokens via an EmailHelper class.