cecilia@nansen.se
Jul 1, 2011
  8745
(4 votes)

Implementing a simple email queue in EPiServer Commerce

Recently I’ve been working with a Commerce project where when a customer has placed an order we wanted to send a confirmation email. In the EPiServer Commerce sample site the code to send the confirmation email is in the code behind of the cart checkout control. This isn’t a good practice as if something goes wrong with the email sending, for example, if the SMTP configuration is incorrect, then the user will see an error saying something went wrong with their order, which in this case is not true.

What we decided to do was to queue up an email request using the EPiServer Dynamic Data Store and then use an EPiServer Scheduled Job to service the email queue and send out the emails.

First of all we created a simple class with the information needed to send the email:

using System;
using EPiServer.Data.Dynamic;

namespace Project.Foundation.CMS.Helpers
{   
    [EPiServerDataStore(AutomaticallyCreateStore = true)]
    public class OrderEmailToBeSent
    {
        public int OrderId { get; set; }
        public Guid Id { get; set; }
    }
}

Then when an order is placed we created an instance of the above class, set it’s properties and saved it in the DDS:

private void SaveOrderId(int orderId)
{
     var store = typeof(OrderEmailToBeSent).GetStore();
     var orderEmailToBeSent = new OrderEmailToBeSent { OrderId = orderId };
     store.Save(orderEmailToBeSent);
}

The Scheduled Job class gets the items from the Dynamic Data Store, loads the order from the Commerce system, sends an email to the customer, and then deletes the item from the DDS:

[ScheduledPlugIn(DisplayName = "Send order emails")]
 class SendOrderEmails
 {
     public static string Execute()
     {
         var store = typeof(OrderEmailToBeSent).GetStore();
         int skip = 0;
         const int count = 50;
         while (true)
         {
             var items = store.Items<OrderEmailToBeSent>().Skip(skip).Take(count);

             if (items.Count() == 0)
                 break;

             foreach (var orderEmailToBeSent in items)
             {
                 var order = CartHelper.FindOrder(orderEmailToBeSent.OrderId);

                 SendEmail(order, "order-purchaseorder-notify");
                 store.Delete(orderEmailToBeSent);
             }

             skip += count;
      }
         return "Success!";
     }
 }

And the code for sending the email:

private static void SendEmail(PurchaseOrder order, string template)
{
      // Add input parameter
      var dic = new Dictionary<string, object>();
      dic.Add("OrderGroup", order);

      // Execute template processor
      var body = TemplateService.Process(template,                  
Thread.CurrentThread.CurrentCulture, dic); // Send out emails var msg = new MailMessage(); msg.From = new MailAddress("store@yourcompany.com"); msg.To.Add(new MailAddress("customer@customer.com", " - " + order.Name)); msg.Subject = "Subject"; msg.Body = body; msg.IsBodyHtml = true; var client = new SmtpClient(); client.Send(msg); }

The TemplateService class used above takes an XSL template and transforms it into an HTML email. See this article for more details about this.

Jul 01, 2011

Comments

Jul 1, 2011 12:28 AM

Awesome!!

Jul 1, 2011 05:59 AM

Very nice!!!

Leif Boström
Leif Boström Jul 1, 2011 08:35 AM

Great post!

Jul 1, 2011 08:46 AM

Super stuff! :-)

Jul 1, 2011 08:05 PM

Good stuff. :)

Jul 1, 2011 08:05 PM

Good stuff. :)

Jul 1, 2011 10:04 PM

Clean and simple, I like it!

cecilia@nansen.se
cecilia@nansen.se Jul 2, 2011 02:25 AM

Thanks! Also thanks to Paul Smith for the idea.

Jul 4, 2011 01:26 PM

Clever thinking. I guess this is a one language solution. In a multilang solution you probably want the email in the users locale, and should store the culture code in the queue too. The scheduler will run with the default locale (web.config)

Jul 4, 2011 05:09 PM

Nice. One thing to think about is that the LINQ expression will be executed twice here. The "Count" will create and execute one LINQ expression against the database, and the foreach will create and execute another LINQ expression against the database.

In this case, it's maby what you want, but I just wanted to post a warning about it.

cecilia@nansen.se
cecilia@nansen.se Jul 4, 2011 05:34 PM

BQ, good point. I should set a flag in the foreach to indicate that there are items instead or use ToList() to convert the LINQ-query to a List.

Please login to comment.
Latest blogs
Integrating Optimizely DAM with Your Website

This article is the second in a series about integrating Optimizely DAM with websites. It discusses how to install the necessary package and code t...

Andrew Markham | Sep 28, 2024 | Syndicated blog

Opticon 2024 - highlights

I went to Opticon in Stockholm and here are my brief highlights based on the demos, presentations and roadmaps  Optimizely CMS SaaS will start to...

Daniel Ovaska | Sep 27, 2024

Required fields support in Optimizely Graph

It's been possible to have "required" properties (value must be entered) in the CMS for a long time. The required metadata haven't been reflected i...

Jonas Bergqvist | Sep 25, 2024

How to write a bespoke notification management system

Websites can be the perfect vehicle for notifying customers of important information quickly, whether it’s the latest offer, an operational message...

Nicole Drath | Sep 25, 2024

Optimizely DAM – An Introduction

I presented a talk about the Optimizely DAM at the OMVP summit during Opticon 2024 in Sweden. I have now converted that talk into two blog posts....

Andrew Markham | Sep 25, 2024 | Syndicated blog

Simple and Effective Personalization with Optimizely Data Platform (ODP)

As we dive into the amazing capabilities of Optimizely One, let’s shine a spotlight on the Optimizely Data Platform (ODP). This simple tool unifies...

Alex Harris - Perficient | Sep 24, 2024 | Syndicated blog