Copied to clipboard

Flag this post as spam?

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


  • Dan 1288 posts 3921 karma points c-trib
    Mar 13, 2017 @ 09:32
    Dan
    0

    Get Umbraco Forms to send plain-text email

    Hi,

    A customer has requested that their email notifications from Umbraco Forms are delivered as plain-text rather than HTML.

    Does anyone know if this is this possible? I'm guessing it probably needs a custom workflow but any pointers would be appreciated.

    Thanks

  • Richard Eyres 98 posts 580 karma points
    Mar 14, 2017 @ 09:39
    Richard Eyres
    101

    Hello

    I would recommend creating your own workflow item and making it a parameter to turn on and off. Here is a little code (from an older project, then i quickly modified). Hopefully it will give you a little starting point.

    public class SendEmail : WorkflowType
    {
        [Setting("Email", description = "Enter the default receiver email, this will be used if a valid email could not be created.", view = "TextField")]
        public string Email { get; set; }
    
        [Setting("Sender Email", description = "Enter the sender email (if blank it will use the settings from /config/umbracosettings.config)", view = "TextField")]
        public string SenderEmail { get; set; }
    
        [Setting("Subject", description = "Enter the subject", view = "TextField")]
        public string Subject { get; set; }
    
        [Setting("Message", description = "Enter the intro message", view = "TextArea")]
        public string Message { get; set; }
    
        [Setting("Send as plain text", description = "Will send the email as a text only email", view = "Checkbox")]
        public string PlainText { get; set; }
    
        [Setting("Attachment", description = "Attach file uploads to email", view = "Checkbox")]
        public string Attachment { get; set; }
    
        public SendEmail()
        {
            this.Id = new Guid("6c7248fd-0258-4ad8-bcef-185789944b18");
            this.Name = "Send Email";
            this.Description = "Will send an email either as HTML or Plain Text.";
        }
    
        public override List<Exception> ValidateSettings()
        {
            List<Exception> exceptions = new List<Exception>();
    
            if (string.IsNullOrEmpty(this.Message))
            {
                exceptions.Add(new Exception("'Message' setting has not been set'"));
            }
    
            return exceptions;
        }
    
        public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)
        {
            var senderEmail = UmbracoConfig.For.UmbracoSettings().Content.NotificationEmailAddress;
            var toEmail = this.Email;
    
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(string.IsNullOrEmpty(this.SenderEmail) ? senderEmail : this.SenderEmail);
            mailMessage.Subject = this.Subject;
            mailMessage.IsBodyHtml = true;
            mailMessage.IsBodyHtml = this.PlainText == true.ToString() ? false : true;
    
            if(string.IsNullOrWhiteSpace(toEmail))
            {
                if (this.Email.Contains(";"))
                {
                    string[] array = this.Email.Split(new char[] { ';' });
                    string[] array2 = array;
                    for (int i = 0; i < array2.Length; i++)
                    {
                        string text = array2[i];
                        mailMessage.To.Add(text.Trim());
                    }
                }
                else
                {
                    mailMessage.To.Add(this.Email);
                }
            }
            else
            {
                mailMessage.To.Add(toEmail);
            }
    
            XmlNode xmlNode = record.ToXml(new XmlDocument());
            XPathNavigator xPathNavigator = xmlNode.CreateNavigator();
            XPathExpression xPathExpression = xPathNavigator.Compile("//fields/child::*");
            xPathExpression.AddSort("@pageindex", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number);
            xPathExpression.AddSort("@fieldsetindex", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number);
            xPathExpression.AddSort("@sortorder", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number);
            XPathNodeIterator xPathNodeIterator = xPathNavigator.Select(xPathExpression);
            string text2 = "<dl>";
            while (xPathNodeIterator.MoveNext())
            {
                text2 = text2 + "<dt><strong>" + DictionaryHelper.GetText(xPathNodeIterator.Current.SelectSingleNode("caption").Value) + ": </strong><dt><dd>";
                XPathNodeIterator xPathNodeIterator2 = xPathNodeIterator.Current.Select(".//value");
                while (xPathNodeIterator2.MoveNext())
                {
                    string text3 = xPathNodeIterator2.Current.Value.Trim();
                    text2 = text2 + DictionaryHelper.GetText(text3).Replace("\n", "<br/>") + "<br/>";
                    if (this.Attachment == true.ToString() && text3.Contains("/forms/upload"))
                    {
                        string fileName = HttpContext.Current.Server.MapPath(text3);
                        mailMessage.Attachments.Add(new Attachment(fileName));
                    }
                }
                text2 += "</dd>";
            }
            text2 += "</dl>";
            mailMessage.Body = "<p>" + this.Message.Replace("\n", "<br/>") + "</p>" + text2;
    
            try
            {
                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Send(mailMessage);
                return WorkflowExecutionStatus.Completed;
            }
            catch (Exception ex)
            {
                LogHelper.Error<Exception>(ex);
                return WorkflowExecutionStatus.Failed;
            }
        }
    }
    
  • Dan 1288 posts 3921 karma points c-trib
    Mar 14, 2017 @ 09:45
    Dan
    0

    Excellent, that's really helpful, thanks.

Please Sign in or register to post replies

Write your reply to:

Draft