Created
May 22, 2015 12:15
-
-
Save TimGeyssens/6ff5392c4d9bbf89bdfc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Umbraco.Forms.Core.Enums; | |
using System.Text.RegularExpressions; | |
using Umbraco.Forms.Data.Storage; | |
using System.Xml; | |
using System.Xml.XPath; | |
using System.Web; | |
using System.Net.Mail; | |
namespace Umbraco.Forms.Core.Providers.WorkflowTypes | |
{ | |
public class SendEmail : WorkflowType | |
{ | |
[Attributes.Setting("Email", description = "Enter the receiver email", view = "TextField")] | |
public string Email { get; set; } | |
[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; } | |
[Attributes.Setting("Subject", description = "Enter the subject", view = "TextField")] | |
public string Subject { get; set; } | |
[Attributes.Setting("Message", description = "Enter the intro message", view = "TextArea" )] | |
public string Message { get; set; } | |
[Attributes.Setting("Attachment", description = "Attach file uploads to email", view = "Checkbox")] | |
public string Attachment { get; set; } | |
public SendEmail() | |
{ | |
this.Id = new Guid("E96BADD7-05BE-4978-B8D9-B3D733DE70A5"); | |
this.Name = "Send email"; | |
this.Description = "Send the result of the form to an email address"; | |
} | |
public override List<Exception> ValidateSettings() | |
{ | |
List<Exception> l = new List<Exception>(); | |
if (string.IsNullOrEmpty(Email)) | |
l.Add(new Exception("'Email' setting has not been set")); | |
if (string.IsNullOrEmpty(Message)) | |
l.Add(new Exception("'Message' setting has not been set'")); | |
return l; | |
} | |
public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e) | |
{ | |
System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage(); | |
m.From = new System.Net.Mail.MailAddress(string.IsNullOrEmpty(SenderEmail) ? umbraco.UmbracoSettings.NotificationEmailSender : SenderEmail); | |
m.Subject = Subject; | |
m.IsBodyHtml = true; | |
if (Email.Contains(';')) | |
{ | |
string[] emails = Email.Split(';'); | |
foreach (string email in emails) | |
{ | |
m.To.Add(email.Trim()); | |
} | |
}else{ | |
m.To.Add(Email); | |
} | |
//RecordsViewer viewer = new RecordsViewer(); | |
XmlNode xml = record.ToXml(new System.Xml.XmlDocument()); //viewer.GetSingleXmlRecord(record, new System.Xml.XmlDocument()); | |
XPathNavigator navigator = xml.CreateNavigator(); | |
XPathExpression selectExpression = navigator.Compile("//fields/child::*"); | |
selectExpression.AddSort("@pageindex", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number); | |
selectExpression.AddSort("@fieldsetindex", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number); | |
selectExpression.AddSort("@sortorder", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number); | |
XPathNodeIterator nodeIterator = navigator.Select(selectExpression); | |
string list = "<dl>"; | |
while (nodeIterator.MoveNext()) | |
{ | |
list += "<dt><strong>" + | |
Umbraco.Forms.Data.DictionaryHelper.GetText( | |
nodeIterator.Current.SelectSingleNode("caption").Value) + ": </strong><dt><dd>"; | |
XPathNodeIterator values = nodeIterator.Current.Select(".//value"); | |
while (values.MoveNext()) | |
{ | |
var val = values.Current.Value.Trim(); | |
list += | |
Umbraco.Forms.Data.DictionaryHelper.GetText(val) | |
.Replace("\n", "<br/>") + "<br/>"; | |
if (this.Attachment == true.ToString() && | |
val.Contains("/forms/upload")) | |
{ | |
// add attachment | |
string filelocation = HttpContext.Current.Server.MapPath(val); | |
m.Attachments.Add(new Attachment(filelocation)); | |
} | |
} | |
list += "</dd>"; | |
} | |
list += "</dl>"; | |
m.Body = "<p>" + Message.Replace("\n","<br/>") + "</p>" + list; | |
System.Net.Mail.SmtpClient s = new System.Net.Mail.SmtpClient(); | |
s.Send(m); | |
return WorkflowExecutionStatus.Completed; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment