Overriding subscription email formatting

by: Mari Jørgensen

 

The SubscriptionMail class in the EPiServer.Personalization namespace is the default subscription handler in EPiServer CMS 5. Being a bit curious to how EPiServer generated the content of the subscription emails, I used .NET Reflector to investigate the content of the SubscriptionMail class.

For each page to be included in the email, the method

FormatPageForBody(PageData subscriptionPage, PageData page)

is responsible for  generating the content, which by default include a  date, a heading, an introduction and a "read more" link. The introduction is based on the following code:

  string str = null;
  if (page["MainIntro"] != null)
  {
     if(page.Property["MainIntro"].Type == 
EPiServer.Core.PropertyDataType.LongString) builder.Append(this.StripHtml(page["PageName"] as string,
page["MainIntro"] as string, 0xff)); else builder.Append(page.Property["MainIntro"].ToWebString()); builder.Append("<br>"); } else { str = this.StripHtml(page["PageName"] as string,
page["MainBody"] as string, 0xff); if (str != null) { builder.Append(str); builder.Append("<br>"); } }

This is all fine as long as the MainIntro property is of type string. If it is of type XHTML and include images and/or links,, the markup will include relative links and both images and links will fail in the email.  The issue can easily be solved by creating your own subscription handler that inherits from EPiServer.Personalization.SubscriptionMail and overrides the method in question. By implementing your own handler you will  have full control of the content. So you can choose to generate emails with html content, or just strip the html from the MainIntro property, as I decided to do.

public class CustomSubscriptionMail : 
EPiServer.Personalization.SubscriptionMail
{ private readonly string _subscriptionMailDatePropertyName; public CustomSubscriptionMail() { this._subscriptionMailDatePropertyName =
"__SubscriptionMailDate"; } public override string FormatPageForBody(
PageData subscriptionPage, PageData page) { StringBuilder builder = new StringBuilder(); builder.Append("<br><span class=MailDate>"); builder.Append("["); builder.Append(((DateTime)page
[this._subscriptionMailDatePropertyName]).
ToShortDateString()); builder.Append("]</span>"); builder.Append("<br><span class=MailPageName>"); builder.Append(page.Property["PageName"].ToWebString()); builder.Append("</span><br><span class=MailPagePreview>"); string str = null; if (page["MainIntro"] != null) { if(page.Property["MainIntro"].Type ==
PropertyDataType.LongString) builder.Append(
this.StripHtml(page["PageName"] as string, page["MainIntro"] as string, 0xff)); else builder.Append(
page.Property["MainIntro"].ToWebString()); builder.Append("<br>"); } else { str = this.StripHtml(page["PageName"] as string, page["MainBody"] as string, 0xff); if (str != null) { builder.Append(str); builder.Append("<br>"); } } builder.Append("</span><a class=MailReadMore href=\""); if (((page.LinkURL != null)
&& page.LinkURL.StartsWith("/")) && !page.LinkURL.StartsWith("//")) { PageReference startPage = PageReference.StartPage; Uri siteUrl = Settings.Instance.SiteUrl; Settings settingsFromPage = DataFactory.Instance.
GetSettingsFromPage(page.PageLink); if (settingsFromPage.PageStartId != startPage.ID) { siteUrl = settingsFromPage.SiteUrl; } builder.Append(siteUrl.GetLeftPart(
UriPartial.Authority)); }
UrlBuilder url = new UrlBuilder( UriSupport.AddLanguageSelection(
page.LinkURL, page.LanguageBranch)); Global.UrlRewriteProvider.ConvertToExternal(
url, page.PageLink, Encoding.UTF8); builder.Append(url.ToString()); builder.Append("\">"); if (subscriptionPage["SubscriptionMailReadMore"]
!= null) { builder.Append(
subscriptionPage["SubscriptionMailReadMore"].
ToString()); } else { builder.Append(LanguageManager.Instance. TranslateFallback("/subscription/pagelinktext", "Read more", subscriptionPage.LanguageBranch)); }
builder.Append("</a>"); builder.Append("<br>"); return builder.ToString(); } private string StripHtml(string title, string html, int len) { if ((title != null) && (html != null)) { if (html.Length > (len * 10)) { html = html.Substring(0, len * 10); } html = html.Replace("<br>", " "); html = html.Replace("<BR>", " "); html = html.Replace("</p>", " "); html = html.Replace("</P>", " "); Regex regex = new Regex("<[^>]*>",
RegexOptions.IgnorePatternWhitespace); string str = HttpUtility.HtmlDecode(
regex.Replace(html, "")).
TrimStart(new char[] { ' ' }); if (str.ToLower().StartsWith(title.ToLower())) { str = str.Remove(0, title.Length).
TrimStart(new char[] { ' ' }); } if (str.Length > len) { str = str.Substring(0, len); } int num = str.LastIndexOfAny(new char[] { ' ', '.' }); if (num > (str.Length / 2)) { str = str.Substring(0, num + 1); } if (str.Length > 0) { return (str + ".."); } } return null; } }

 

After you have added the class to you project and made a rebuild, remember to change to subscriptionHandler setting in your web.config file to use your new handler.

21 May 2008


Comments

  1. Sweet!! Was just researching the topic and found this help full post!
Post a comment    
User verification Image for user verification  
EPiTrace logger