Copied to clipboard

Flag this post as spam?

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


  • Bo Jacobsen 438 posts 1818 karma points
    Jun 03, 2020 @ 08:29
    Bo Jacobsen
    1

    How to override SendRazorEmail to use a custom message like the sendEmail

    Hi all.

    Using umbraco 7.14.5 and Umbraco Forms 7.4.1.

    I would like to be able to add a message to the SendRazorEmail workflow that an editor can edit.

    So i tried to override the SendRazorEmail and add a Message attribute. But i cant figure out how to get it into the .cshtml that @inherits UmbracoViewPage<Umbraco.Forms.Core.Models.FormsHtmlModel>

    public class SendCustomEmailWorkflow : Umbraco.Forms.Core.Providers.WorkflowTypes.SendRazorEmail
    {
        public SendCustomEmailWorkflow()
        {
            this.Name = "Send Custom Email";
            this.Id = new Guid("D6A2C406-CF89-11DE-B075-55B055D89593");
            this.Description = "Used for designed emails";
        }
    
        [Umbraco.Forms.Core.Attributes.Setting("Message", description = "insert a custom message, where {alias} replace data from the field with the alias.", view = "textarea")]
        public string Message { get; set; }
    }
    

    Maybe there is a way to get this workflow by the forms id?

    Do anyone have any idear?

  • Bo Jacobsen 438 posts 1818 karma points
    Jun 04, 2020 @ 08:23
    Bo Jacobsen
    0

    I manged to do a work around where i made a complete new WorkFlow and used RazorEngine.

    Simplified WorkFlow

    public class SendCustomEmailWorkflow : WorkflowType
    {
        public SendCustomEmailWorkflow()
        {
            this.Name = "Send Custom Email";
            this.Id = new Guid("D6A2C406-CF89-11DE-B075-55B055D89593");
            this.Description = "Has designed email";
            this.Icon = "icon-wand color-green";
        }
    
        [Setting("Receiver Email", description = "Enter the receiver email", view = "textfield")]
        public string ReceiverEmail { get; set; }
    
        [Setting("Sender Email", description = "Enter the sender email", view = "textfield")]
        public string SenderEmail { get; set; }
    
        [Setting("Subject", description = "Enter the subject", view = "textfield")]
        public string Subject { get; set; }
    
        [Setting("Message", description = "Enter a message", view = "textarea")]
        public string Message { get; set; }
    
        // Could add a template picker here to make it more dynamic.
    
        public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)
        {
            // This is my custom model, where i transform the Record into something like the FormsHtmlModel.
            var model = new FormRecordModel(record, this.Message);
    
            // RazorEngine 3.10.0
            var viewsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Views\\Partials");
            var config = new TemplateServiceConfiguration
            {
                TemplateManager = new ResolvePathTemplateManager(new[] { viewsPath }),
                DisableTempFileLocking = true,
            };
            Engine.Razor = RazorEngineService.Create(config);
            var html = Engine.Razor.RunCompile("Custom-Email.cshtml", null, model);
    
            // After this i send the email via SmtpClient
    
            return WorkflowExecutionStatus.Completed;
        }
    
        public override List<Exception> ValidateSettings()
        {
            var list = new List<Exception>();
            if (string.IsNullOrEmpty(this.ReceiverEmail))
            {
                list.Add(new Exception("Reciever Email is required, but is empty"));
            }
            if (string.IsNullOrEmpty(this.SenderEmail))
            {
                list.Add(new Exception("Sender Email is required, but is empty"));
            }
            if (string.IsNullOrEmpty(this.Subject))
            {
                list.Add(new Exception("Subject is required, but is empty"));
            }
            if (string.IsNullOrEmpty(this.Message))
            {
                list.Add(new Exception("Message is required, but is empty"));
            }
            return list;
        }
    }
    

    Simplified custom-email.html

    @using RazorEngine.Text
    @using Umbraco.Core.Models
    @using Umbraco.Web
    @model FormRecordModel
    @{
        var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    }
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    </head>
    <body>
        <table>@(new RawString(Model.Message))</table>
    </body>
    </html>
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies