Sending emails is always one of the jobs that most customers want. Sometimes they want to send a welcome letter or some notifications or even a newsletter for the customers and so they ask for a module that they can use. Writing a method for sending emails takes less than 10 seconds these days (
SmtpClient.Send :) ) but creating a good content that can be flexible enough for the client to use and at the same time not much complex that make them suffer is a bit tricky. So In this post I will show you how to do it in a simple way.
Dividing the content
If you ask a client what content will you have in your emails, they will probably say everything is possible but they usually want to have the same design on most of their emails. It is a way to show that all of those emails are from the same company. Also, the one who writes the email content usually doesn't have any knowledge about the design, and you don't want to let him mess up with the CSS.
So in short you have these contents inside your email engine:
1- A HTML/CSS design form
2- Some placeholders for special data like customer name, Date, etc.
3- Some content that changes by the editor
Using MVC engine
Usually whenever we talk about HTML/CSS design, we think of how we do it using MVC, because as web developers that is what we do. By using MVC, we always separate our business from the view. Right? We never write anything inside the view so if the customer doesn't like the look of his pages, or if there was a change in theme or etc. it could be possible without us doing anything. And let me add that doing the front-end usually needs a good eye to see what is pretty and I myself don't have it :)
So, why not using MVC engine to create our email body? It is only a simple HTML page noting more.
Replacing the placeholders
String processing is very easy in all programming languages, you just need to define strings that no one will use in a text. Something simple like having names with double hash in both sides like ##SomeName## will probably do the task. You can also use the simple string.Replace method but for the sake of both speed and flexibility I choose to work with RegEx library most of the time.
Implementing the code
Rendering the MVC view to string:
There is a very good post by Rick Strahl . He basically wrote a class to help him do some of the stuff easily:
public class ViewRenderer { protected ControllerContext Context { get; set; } public ViewRenderer(ControllerContext controllerContext = null) { if (controllerContext == null) { if (HttpContext.Current != null) controllerContext = CreateController().ControllerContext; else throw new InvalidOperationException( "ViewRenderer must run in the context of an ASP.NET " + "Application and requires HttpContext.Current to be present."); } Context = controllerContext; } public string RenderView(string viewPath, object model) { return RenderViewToStringInternal(viewPath, model, false); } public string RenderPartialView(string viewPath, object model) { return RenderViewToStringInternal(viewPath, model, true); } public static string RenderView(string viewPath, object model, ControllerContext controllerContext) { ViewRenderer renderer = new ViewRenderer(controllerContext); return renderer.RenderView(viewPath, model); } public static string RenderPartialView(string viewPath, object model, ControllerContext controllerContext) { ViewRenderer renderer = new ViewRenderer(controllerContext); return renderer.RenderPartialView(viewPath, model); } protected string RenderViewToStringInternal(string viewPath, object model, bool partial = false) { ViewEngineResult viewEngineResult = null; if (partial) viewEngineResult = ViewEngines.Engines.FindPartialView(Context, viewPath); else viewEngineResult = ViewEngines.Engines.FindView(Context, viewPath, null); if (viewEngineResult == null) throw new FileNotFoundException("View did not find!"); var view = viewEngineResult.View; Context.Controller.ViewData.Model = model; string result = null; using (var sw = new StringWriter()) { var ctx = new ViewContext(Context, view, Context.Controller.ViewData, Context.Controller.TempData, sw); view.Render(ctx, sw); result = sw.ToString(); } return result; } public static T CreateController (RouteData routeData = null) where T : Controller, new() { T controller = new T(); HttpContextBase wrapper = null; if (HttpContext.Current != null) wrapper = new HttpContextWrapper(System.Web.HttpContext.Current); if (routeData == null) routeData = new RouteData(); if (!routeData.Values.ContainsKey("controller") && !routeData.Values.ContainsKey("Controller")) routeData.Values.Add("controller", controller.GetType().Name .ToLower() .Replace("controller", "")); controller.ControllerContext = new ControllerContext(wrapper, routeData, controller); return controller; } }
Creating the viewModel
To work with MVC, you need to have a model to pass to the view and then in the view the designer knows what he has to create a good view.
Create a EmailTemplateBase so you can use it for different views depending on what you need to do and put your base stuff inside that. something like:
public class EmailTemplateBase { public string Markup { get; set; } public string BodyTextMarkup { get; set; } }
Then you can inherit from it to have your special view or just use it as your view model.
Creating the Controller
Since we are using the .Net MVC, it is not possible to generate stuff without using a controller. So just create a simple controller:
public class mycontroller { public ActionResult Index(EmailTemplateBase model) { return View(model); } }
Creating the view
It is the simple Razor MVC view and the viewmodel is the one that you've already created so I won't bother writing that :)
Replacing the name value tokens
I myself prefer to have this method in a helper class with extension methods so I can use it like text.ReplaceTokens(collection) but you can use it as a simple method without extentions.
public static string ReplaceTokens(this string text, NameValueCollection nameValueCollection) { foreach (var key in nameValueCollection.AllKeys) { var replacementText = nameValueCollection[key] ?? ""; text = Regex.Replace(text, key, replacementText, RegexOptions.IgnoreCase); } return text; }Creating the Email Body
To create the email body, you need to get the body markup from the DB where you put bodytext that you already took from the editor. also you need to have a list of namevalues that you want to replace somewhere. The namevalue collection has to have everything that is possible for the editor to use, but it has to be ok for him not using some or all of them.
public string CreateEmail(string bodyMarkupText, NameValueCollection tokenCollection) { var url = "~/views/myPage/Index.cshtml"; var model = new ReceiptViewModel() { BodyTextMarkup = bodyMarkupText }; var bodyText = ViewRenderer.RenderView(url, model, null); return bodyText.ReplaceTokens(tokenCollection); }
Easy right?
Have fun :)
Nice
ReplyDeleteInteresting rread
ReplyDelete