SHARE
TWEET

Umbraco Forms - Reply To, BCC, Extended Send Email, Workflow

Haydzyo Aug 30th, 2017 709 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Net.Mail;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text.RegularExpressions;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using System.Xml;
  9. using System.Xml.XPath;
  10. using Umbraco.Forms.Core;
  11. using Umbraco.Forms.Core.Attributes;
  12. using Umbraco.Forms.Core.Enums;
  13. using Umbraco.Forms.Data.Storage;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18.  
  19. namespace Contour.SharedSource.Providers.WorkflowTypes
  20. {    
  21.     public class SendEmailExtended : WorkflowType
  22.     {
  23.         [Umbraco.Forms.Core.Attributes.Setting("Email", description = "Enter the receiver email", view = "TextField")]
  24.         public string Email { get; set; }
  25.  
  26.         [Umbraco.Forms.Core.Attributes.Setting("SenderEmail", description = "Enter the sender email (if blank it will use the settings from /config/umbracosettings.config)", view = "TextField")]
  27.         public string SenderEmail { get; set; }
  28.  
  29.         [Umbraco.Forms.Core.Attributes.Setting("BCC", description = "Enter a BCC email address", view = "TextField")]
  30.         public string BCC { get; set; }
  31.  
  32.         [Umbraco.Forms.Core.Attributes.Setting("Reply To", description = "Enter a Reply-To email address", view = "TextField")]
  33.         public string replyTo { get; set; }
  34.  
  35.         [Umbraco.Forms.Core.Attributes.Setting("Subject", description = "Enter the subject", view = "TextField")]
  36.         public string Subject { get; set; }
  37.  
  38.         [Umbraco.Forms.Core.Attributes.Setting("Message", description = "Enter the intro message",  view = "TextArea" )]
  39.         public string Message { get; set; }
  40.  
  41.        
  42.         public SendEmailExtended()
  43.         {
  44.             this.Id = new Guid("FC92552F-4063-4CC2-ACC9-B1DF7EEA151A");
  45.             this.Name = "Send Email Extended";
  46.             this.Description = "An extenstion of send email with functionality to BCC & Reply-To";
  47.             this.Icon = "icon-message";
  48.         }
  49.  
  50.         public override List<Exception> ValidateSettings()
  51.         {
  52.             List<Exception> l = new List<Exception>();
  53.             if (string.IsNullOrEmpty(Email))
  54.                 l.Add(new Exception("'Email' setting not filled out'"));
  55.            
  56.             if (string.IsNullOrEmpty(Message))
  57.                 l.Add(new Exception("'Message' setting not filled out'"));
  58.  
  59.  
  60.             return l;
  61.         }
  62.  
  63.         public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)
  64.         {
  65.             System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
  66.             m.From = new System.Net.Mail.MailAddress(umbraco.UmbracoSettings.NotificationEmailSender);
  67.             m.Subject = Subject;
  68.             m.IsBodyHtml = true;
  69.  
  70.             // ATTACH EMAIL ADDRESS'
  71.             if (Email.Contains(';'))
  72.             {
  73.                 string[] emails = Email.Split(';');
  74.                 foreach (string email in emails)
  75.                 {
  76.                     m.To.Add(email.Trim());
  77.                 }
  78.             }else{
  79.                 m.To.Add(Email);      
  80.             }
  81.  
  82.             // ATTACH BCC ADDRESS'
  83.             if (!string.IsNullOrEmpty(BCC)) { m.Bcc.Add(BCC); }
  84.            
  85.             // ATTACH REPLY TO ADDRESS'
  86.             if (!string.IsNullOrEmpty(replyTo)) { m.ReplyToList.Add(replyTo); }
  87.  
  88.             /*RecordsViewer viewer = new RecordsViewer();
  89.             XmlNode xml = viewer.GetSingleXmlRecord(record, new System.Xml.XmlDocument());*/
  90.            
  91.             var xml = record.ToXml(new System.Xml.XmlDocument());
  92.             XPathNavigator navigator = xml.CreateNavigator();
  93.  
  94.             XPathExpression selectExpression = navigator.Compile("//fields/child::*");
  95.             selectExpression.AddSort("@pageindex", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number);
  96.             selectExpression.AddSort("@fieldsetindex", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number);
  97.             selectExpression.AddSort("@sortorder", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number);
  98.            
  99.             XPathNodeIterator nodeIterator = navigator.Select(selectExpression);
  100.  
  101.             string list = "<dl>";
  102.  
  103.             while( nodeIterator.MoveNext() ){
  104.  
  105.                 list += "<dt><strong>" + nodeIterator.Current.SelectSingleNode("caption").Value + ": </strong><dt><dd>";
  106.                
  107.                 XPathNodeIterator values = nodeIterator.Current.Select(".//value");
  108.                
  109.                 while(values.MoveNext())
  110.                     list += values.Current.Value.Trim() + "<br/>";
  111.                
  112.                 list += "</dd>";
  113.  
  114.             }
  115.  
  116.             list += "</dl>";
  117.  
  118.             m.Body = "<p>" + Message + "</p>" + list;
  119.  
  120.             System.Net.Mail.SmtpClient s = new System.Net.Mail.SmtpClient();
  121.  
  122.             s.Send(m);
  123.                        
  124.             return WorkflowExecutionStatus.Completed;
  125.         }
  126.     }
  127. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top