Use Razor for Email Template outside ASP.NET MVC

So the ASP.NET MVC 3 RTM is out and the real icon of this release is the Razor View Engine. The best thing of Razor is that unlike its predecessor(webforms) it is not tied with the web environment, we can easily host it outside the web and  use it as template engine for various purpose. The project that I am currently working on needs to send bulk emails at various point of the application,  since the numbers can be hundreds or literary thousands I want to host it outside the web application so that it does add unnecessary overheads to the web application. In this post, I will show you you the code that I am using to generate these mails, you can download the complete code with MSpec specs from the bottom of the post.

Lets say, in your application you want to send a welcome mail to user, whenever s/he registers. To send the mail you can use the following code:


              public virtual void SendWelcomeMail(string name, string password, string email)
              {
                  var model = new
                              {
                                  From = Configuration.FromAddress,
                                  To = email,
                                  Name = name,
                                  Password = password,
                                  LogOnUrl = Configuration.LogOnUrl()
                              };
              
                  var mail = TemplateEngine.Execute(Configuration.SendWelcomeMailTemplateName, model);
              
                  Sender.Send(mail);
              }
              

And here is the template that it will use to generate the actual mail content:


              @{
                  From = Model.From;
                  To.Add(Model.To);
                  Subject = "Welcome to my mysite.com";
              }
              <html>
              <head>
                  <title>Welcome to mysite.com</title>
              </head>
              <body>
                  <p>Dear @Model.Name,</p>
                  <p>An account has been created for you.</p>
                  <p>Your account is FREE and allows you to perform bla bla features.</p>
                  <p>To login and complete your profile, please go to:</p>
                  <p><a href="@Model.LogOnUrl">@Model.LogOnUrl</a></p>
                  <p>Your User ID is your email address and password is: @Model.Password</p>
              </body>
              </html>
              

The code is very simple, first we are creating an anonymous object as model, you can also use strongly typed as well as the new dynamic object, next we are using the template engine to generate the output and at last the mail object is passed to the mail sender for dispatching it. In the template the model is that we passed from the method is exposed as the dynamic object. Behind the scene the template uses a base class which has some of the common properties that we often use for setting the mail:


              public abstract class EmailTemplate : IEmailTemplate
              {
                  private readonly StringBuilder buffer;
              
                  [DebuggerStepThrough]
                  protected EmailTemplate()
                  {
                      To = new List<string>();
                      ReplyTo = new List<string>();
                      CC = new List<string>();
                      Bcc = new List<string>();
                      Headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
              
                      buffer = new StringBuilder();
                  }
              
                  public string From { get; set; }
              
                  public string Sender { get; set; }
              
                  public ICollection<string> To { get; private set; }
              
                  public ICollection<string> ReplyTo { get; private set; }
              
                  public ICollection<string> CC { get; private set; }
              
                  public ICollection<string> Bcc { get; private set; }
              
                  public IDictionary<string, string> Headers { get; private set; }
              
                  public string Subject { get; set; }
              
                  public string Body
                  {
                      get { return buffer.ToString(); }
                  }
              
                  protected dynamic Model { get; private set; }
              
                  public void SetModel(dynamic model)
                  {
                      Model = model;
                  }
              
                  public abstract void Execute();
              
                  public virtual void Write(object value)
                  {
                      WriteLiteral(value);
                  }
              
                  public virtual void WriteLiteral(object value)
                  {
                      buffer.Append(value);
                  }
              }
              

It is also possible to compose a mail which contains both text and html format, so that the mail client which supports html should use the html version and the unsupported client could use the plain version. To compose a multi formatted mail you will have to use three templates the shared template will contain the common properties like From/To/Subject etc. and the other two will contain the actually body. For example, to use the above mail in multi format we will create the following templates:

Shared Template

              @{
                  From = Model.From;
                  To.Add(Model.To);
                  Subject = "Welcome to mysite.com";
              }
              
Html Template

              <html>
              <head>
                  <title>Welcome to mysite.com</title>
              </head>
              <body>
                  <p>Dear @Model.Name,</p>
                  <p>An account has been created for you.</p>
                  <p>Your account is FREE and allows you to perform bla bla features.</p>
                  <p>To login and complete your profile, please go to:</p>
                  <p><a href="@Model.LogOnUrl">@Model.LogOnUrl</a></p>
                  <p>Your User ID is your email address and password is: @Model.Password</p>
              </body>
              </html>
              
Text Template

              Dear @Model.Name,
              
              An account has been created for you.
              
              Your account is FREE and allows you to perform bla bla features.
              
              To login and complete your profile, please go to:
              
              @Model.LogOnUrl
              
              Your User ID is your email address and password is: @Model.Password
              

Now, when the mail is sent it should look like the following:


              X-Sender: me@myself.com
              X-Receiver: jon@smith.com
              MIME-Version: 1.0
              From: me@myself.com
              To: jon@smith.com
              Date: 15 Jan 2011 12:42:41 +0600
              Subject: Welcome to mysite.com
              Content-Type: multipart/alternative;
               boundary=--boundary_0_9202367d-977d-49b5-ab86-2bb3ed1bfc76
              
              
              ----boundary_0_9202367d-977d-49b5-ab86-2bb3ed1bfc76
              Content-Type: text/html
              Content-Transfer-Encoding: base64
              
              PGh0bWw+DQo8aGVhZD4NCiAgICA8dGl0bGU+V2VsY29tZSB0byBteXNpdGUuY29t
              PC90aXRsZT4NCjwvaGVhZD4NCjxib2R5Pg0KICAgIDxwPkRlYXIgSm9uIFNtaXRo
              LDwvcD4NCiAgICA8cD5BbiBhY2NvdW50IGhhcyBiZWVuIGNyZWF0ZWQgZm9yIHlv
              dS48L3A+DQogICAgPHA+WW91ciBhY2NvdW50IGlzIEZSRUUgYW5kIGFsbG93cyB5
              b3UgdG8gcGVyZm9ybSBibGEgYmxhIGZlYXR1cmVzLjwvcD4NCiAgICA8cD5UbyBs
              b2dpbiBhbmQgY29tcGxldGUgeW91ciBwcm9maWxlLCBwbGVhc2UgZ28gdG86PC9w
              Pg0KICAgIDxwPjxhIGhyZWY9Imh0dHA6Ly9teWNvbXBhbnkuY29tL2xvZ29uIj5o
              dHRwOi8vbXljb21wYW55LmNvbS9sb2dvbjwvYT48L3A+DQogICAgPHA+WW91ciBV
              c2VyIElEIGlzIHlvdXIgZW1haWwgYWRkcmVzcyBhbmQgcGFzc3dvcmQgaXM6IH4h
              QWdjMmQjNzwvcD4NCjwvYm9keT4NCjwvaHRtbD4=
              ----boundary_0_9202367d-977d-49b5-ab86-2bb3ed1bfc76
              Content-Type: text/plain
              Content-Transfer-Encoding: base64
              
              RGVhciBKb24gU21pdGgsDQoNCkFuIGFjY291bnQgaGFzIGJlZW4gY3JlYXRlZCBm
              b3IgeW91Lg0KDQpZb3VyIGFjY291bnQgaXMgRlJFRSBhbmQgYWxsb3dzIHlvdSB0
              byBwZXJmb3JtIGJsYSBibGEgZmVhdHVyZXMuDQoNClRvIGxvZ2luIGFuZCBjb21w
              bGV0ZSB5b3VyIHByb2ZpbGUsIHBsZWFzZSBnbyB0bzoNCg0KaHR0cDovL215Y29t
              cGFueS5jb20vbG9nb24NCg0KWW91ciBVc2VyIElEIGlzIHlvdXIgZW1haWwgYWRk
              cmVzcyBhbmQgcGFzc3dvcmQgaXM6IH4hQWdjMmQjNw==
              ----boundary_0_9202367d-977d-49b5-ab86-2bb3ed1bfc76--
              

Currently it uses templates which are stored in the file system, but it is extensible enough to store the templates in other mediums like database. Like ASP.NET it compiles the templates to .NET types when first time it is requested, but it does not have the cache invalidation support like asp.net, may be I will add it in future.

That’s it for today.

Download: EmailTemplate.zip

Shout it

Comments

blog comments powered by Disqus