Send Plain Text and Rich Text Emails with Active Commerce

Follow

Active Commerce contains a feature that allows you to send plain text and rich text emails, including the use of a Sitecore item to be used as the template for your message.

To start, insert a new Mail Template under /sitecore/content/<your site>/Site Settings/MailTemplates. Provide the from address, to address and subject. If any data is dynamic in those fields, surround a key name with brackets like this:

{FromAddress}

The key name you choose is arbitrary and will map to a parameter list. More on that below.

Next provide your body copy in the Body field. This text can also contain dynamic pieces using key names.

Do you prefer a more rich text body source? Insert an Email Body item under "/sitecore/content/<your site>/Site Settings/Rich Mail Templates" and set the BodySource field on the Mail Template to your new Email Body item.

Active Commerce will render your new Email Body item and scrape the contents to be used as the body. You'll see you have the ability to add logos, promos and footers to your message.

You can use Active Commerce's Mail-Generic.ascx sublayout for your Email Body item, or create your own. To build your own, create a new user control that inheriets from ActiveGlassUserControl<ActiveCommerce.Content.Pages.EmailBody>.

Your code behind should look like this:

using System;
using ActiveCommerce.Content.Pages;

namespace YourNamespace.Web.skins
{
    public partial class MyMail: ActiveGlassUserControl<EmailBody>
    {
        protected virtual void Page_Load(object sender, EventArgs e)
        {
            if (!Sitecore.Context.PageMode.IsNormal)
            {
                Model.PopulateUnformatted();
            }
            else
            {
                Model.PopulateFormatted(Request.QueryString);
            }
        }
    }
}

Notice the "Model.PopulateFormatted" call above. Your Email Body item's Title, Message, Footnote and Footer can all use dynamic data as described above.

Now that your message structures are built, it's time to actually send a message as shown in this example for a shipping confirmation email: 

public static void SendShipConfirmEmail(string recipient, string orderNumber, string trackingUrl)
        {
            try
            {
                var parameters = new
                {
                    Recipient = recipient,
                    OrderNum = orderNumber,
                    TrackUrl = trackingUrl
                };
                var mail = Context.Entity.Resolve(<IMail>);
                mail.SendMail("Order Shipping Confirmation", parameters, parameters.ToQueryString() /* Make all parameters available for Body Source */);
            }
            catch (Exception x)
            {
                Log.Error("Send Ship Confirm Email Error", x, this);
            }
        }

What's going on here? 

  • Notice the Parameters collection. These are the keys that will populate the dynamic pieces of data we outlined in the email templates.
  • We're resolving IMail to process and send the message.
  • In this example, "Order Shipping Confirmation" is the name of the Mail Template we want to use to send a message. You can also use the Sitecore item GUID here.

Don't want to keep your mail template in Sitecore?

IMail.SendMail has an overload that accepts the to address, from address, subject and body to send a message out. This might be helpful if a third party is providing the copy for the message.

Have more questions? Submit a request

Comments