Skip to content

Instantly share code, notes, and snippets.

@TimGeyssens
Created May 11, 2015 13:17
Show Gist options
  • Save TimGeyssens/3fffc0a4a6e457a0cd08 to your computer and use it in GitHub Desktop.
Save TimGeyssens/3fffc0a4a6e457a0cd08 to your computer and use it in GitHub Desktop.
Contour send email workflow
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Web;
using Umbraco.Forms.Core.Enums;
using System.Text.RegularExpressions;
using Umbraco.Forms.Data.Storage;
using System.Xml;
using System.Xml.XPath;
namespace Umbraco.Forms.Core.Providers.WorkflowTypes
{
public class SendEmail : WorkflowType
{
[Attributes.Setting("Email", description = "Enter the receiver email", control = "Umbraco.Forms.Core.FieldSetting.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)", control = "Umbraco.Forms.Core.FieldSetting.TextField")]
public string SenderEmail { get; set; }
[Attributes.Setting("SenderName", description = "Enter the sender name", control = "Umbraco.Forms.Core.FieldSetting.TextField")]
public string SenderName { get; set; }
[Attributes.Setting("Subject", description = "Enter the subject", control = "Umbraco.Forms.Core.FieldSetting.TextField")]
public string Subject { get; set; }
[Attributes.Setting("Message", description = "Enter the intro message", control = "Umbraco.Forms.Core.FieldSetting.TextArea" )]
public string Message { get; set; }
[Umbraco.Forms.Core.Attributes.Setting("Attachment", description = "Attach file uploads to email", control = "Umbraco.Forms.Core.FieldSetting.Checkbox")]
public string Attachment { get; set; }
public SendEmail()
{
this.Id = new Guid("cffe8f28-e7a6-44db-95aa-5acdccee470f");
this.Name = "Send email (with sender name option)";
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, SenderName);
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() ){
XPathNavigator node = nodeIterator.Current.SelectSingleNode(".//value");
if (this.Attachment == true.ToString() && node != null &&
node.Value.Contains("/umbraco/plugins/umbracoContour/files/"))
{
// add attachment
string filelocation = HttpContext.Current.Server.MapPath(node.Value);
m.Attachments.Add(new Attachment(filelocation));
}
else
{
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())
list +=
Umbraco.Forms.Data.DictionaryHelper.GetText(values.Current.Value.Trim())
.Replace("\n", "<br/>") + "<br/>";
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