Copied to clipboard

Flag this post as spam?

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


  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Jul 04, 2015 @ 13:03
    Dennis Adolfi
    0

    Umbraco Forms - add footer HTML in the bottom of every email being sent out using the "Send email" workflow.

    Hello partypeople, i have a question concerning Umbraco Forms.

    I have a Umbraco 7.2 site set up with Umbraco Forms. I currently have about 10 different Forms set up and its going to be a lot more.

    What i want to do i add footer HTML (you know basic company image and some address info) in the bottom of every email being sent out, and i want to do this by using the "Send email" workflow type.

    The reason i want to do this using the "Send email" type and not the "Send xslt transformed email" type is because every Form is different and concerns different areas (One form is for "Contact us" emails, another one for "Order our book" emails, another one is for "Subscribe to our newsletter" emails and so on and so on..) and i dont want to be doing different XSLT files for every different form.

    The "Send email" type has the "Message" field wich my editors can edit and put the content of every email into, and all i want to put a footer to every email. If the "Send as xslt transformed email" type had the "Message" field i could have just made ONE xslt template and used that for every form, but since that not a option, i would have to make one for every different type of form.

    So, dont awnser this by saying "Use the xslt transformed email", and please dont awnser it with "It cant be done". I need a nifty developer who´ve figured out a hack or a way for doing this. This feels like a pretty standard thing that i expected would have already existed in the Umbraco Forms package.

    Thanks for your help!

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Jul 24, 2015 @ 11:29
    Dennis Adolfi
    101

    Solved this by creating my own WorkflowType, givit it the same Settings as a the "Send email" WorkflowType, but then i could make my own Workflow Execution. Then i was able to add a html structure surrounding the "Message" setting and include my footer and custom css.

    Post´s code in case somone runs in to the same scenario:

    CustomWorkflow.cs

    public class CustomWorkflow : Umbraco.Forms.Core.WorkflowType
    {
        public CustomWorkflow()
        {
            this.Name = "Send email with custom stylesheet and html structure.";
            this.Id = new Guid("91517EB2-8D61-483C-A3DA-7850FCE05123");
            this.Description = "Sends a email with a custom stylesheet and html structure.";
        }
    
        [Umbraco.Forms.Core.Attributes.Setting("Email", description = "Enter the receiver email", view = "TextField")]
        public string Email { get; set; }
    
        [Umbraco.Forms.Core.Attributes.Setting("SenderEmail", 
            description = "Enter the sender email (if blank it will use the settings from /config/umbracosettings.config)",view = "TextField")]
        public string SenderEmail { get; set; }
    
        [Umbraco.Forms.Core.Attributes.Setting("Subject", description = "Enter the subject", view = "TextField")]
        public string Subject { get; set; }
    
        [Umbraco.Forms.Core.Attributes.Setting("Message", description = "Enter the intro message", view = "TextArea")]
        public string Message { get; set; }
    
        public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)
        {
            if (string.IsNullOrEmpty(this.SenderEmail))
            {
                // Gets the Notification EmailAddress from the umbracosettings.config file.
                this.SenderEmail = UmbracoConfig.For.UmbracoSettings().Content.NotificationEmailAddress;
            }
    
            if (record.RecordFields != null && record.RecordFields.Values.Any())
            {
                // To send Email to the visitor, enter {alias} and get field value from the form.
                if (this.Email.ToLower().Contains("{") && record.RecordFields.Values.Any(p => p.Alias == (this.Email.ToLower().TrimStart('{').TrimEnd('}'))))
                    this.Email = record.RecordFields.Values.First(p => p.Alias == (this.Email.ToLower().TrimStart('{').TrimEnd('}'))).ValuesAsString();
    
                this.Message += "<p>";
                foreach (var recordField in record.RecordFields.Values)
                {
                    this.Message += "<strong>" + recordField.Field.Caption + ":</strong>&nbsp;<span>" + recordField.ValuesAsString() + "</span><br/>";
                }
                this.Message += "</p>";
            }
    
            var mailhandler = new MailHandler();
            mailhandler.SendEmail(this.SenderEmail, this.Email, this.Subject, this.Message, null);
    
            return WorkflowExecutionStatus.Completed;
        }
    
        public override List<Exception> ValidateSettings()
        {
            var exceptions = new List<Exception>();
    
            if (string.IsNullOrEmpty(this.Email))
                exceptions.Add(new Exception("Please enter Email!"));
    
            if (string.IsNullOrEmpty(this.Subject))
                exceptions.Add(new Exception("Please enter Subject!"));
    
            return exceptions; 
        }
    }
    

    MailHandler.cs

    public class MailHandler
    {
        public bool SendEmail(string from, string to, string subject, string message, List<Attachment> attachments)
        {
            try
            {
                var av = EmailTemplateConverter.GetMailTemplateWithLogo(message);
    
                using (var client = new SmtpClient())
                {
                    using (var msg = new MailMessage(from, to, subject, message)
                    {
                        IsBodyHtml = true,
                        BodyEncoding = Encoding.GetEncoding(1252),
                        Sender = new MailAddress(from, to)
    
                    })
                    {
                        if (attachments != null)
                            attachments.ForEach(p => msg.Attachments.Add(p));
    
                        msg.AlternateViews.Add(av);
    
                        client.Send(msg);
                        return true;
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
    

    I didnt manage to post the EmailTemplateConverter class in this post, but its pretty standard. Google it or i can send it to whoever is interested.

Please Sign in or register to post replies

Write your reply to:

Draft