Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nekesh 13 posts 103 karma points
    Aug 25, 2016 @ 14:14
    Nekesh
    0

    Email template

    I am trying to create template application. In which I am sending umbraco content template in email. But it seems Umbraco templates are not responsive with in email applications.

    Can any body suggest how should I design template in Umbraco for email content.

  • Casper 70 posts 308 karma points
    Aug 25, 2016 @ 16:26
    Casper
    1

    Not sure i'm getting it right, but here goes. If you wan' to be able to create views in Umbraco backend and render them as (html)string for sending as email then you could do like so (note that this is not bound to any Umbraco stuff, so you could do the same from any other asp.net app):

    Create a model that hold data you need:

    public class EmailViewModel
    {
        public string Recipient { get; set; }
        public string Sender { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public string Signature { get; set; }
    
        ...
    
    }
    

    Create a controller that will return a view populated with your model

    public class DefaultEmailController : Controller
    {
        public ActionResult Index(EmailViewModel model)
        {
            return View(model);
        }
    }
    

    Create one more controller - we will need this to satisfy ControllerContext in below method // public class EmptyController : ControllerBase { protected override void ExecuteCore() { } }

    Create the method that will render the view to string

    public static class EmailHelper
    {
        public static string RenderViewToString(string controllerName, string viewName, object viewData)
        {
            var context = HttpContext.Current;
    
            var contextBase = new HttpContextWrapper(context);
    
            var routeData = new RouteData();
            routeData.Values.Add("controller", controllerName);
    
            var controllerContext = new ControllerContext(contextBase, routeData, new EmptyController());
    
            var razorViewEngine = new RazorViewEngine();
    
            var razorViewResult = razorViewEngine.FindView(controllerContext, viewName, "", false);
    
            var writer = new StringWriter();
    
            var viewContext = new ViewContext(controllerContext, razorViewResult.View, new ViewDataDictionary(viewData), new TempDataDictionary(), writer);
    
            razorViewResult.View.Render(viewContext, writer);
    
            return writer.ToString();
        }}
    

    Now we need a view that will consume our model:

    @{ Layout = "~/Views/YOUR-MASTER-PAGE.cshtml"; }
    @inherits System.Web.Mvc.WebViewPage<EmailViewModel>
    Hello @model.Recipient
    

    Ok, lets create mail:

    var model = new EmailViewModel() { ... };
    
    var emailText = EmailHelper.RenderViewToString("YOUR-CONTROLLER",  "YOUR VIEW", model);
    

    // casper

  • Thomsen 113 posts 336 karma points
    Aug 08, 2018 @ 13:37
    Thomsen
    0

    Thank you! This post was a lifesaver ... Have been struggeling with this for days.

  • Yakov Lebski 590 posts 2344 karma points
    Aug 25, 2016 @ 17:50
    Yakov Lebski
    0

    I can recommend PerplexMail package, it's make sending email and template managing very easy

Please Sign in or register to post replies

Write your reply to:

Draft