Email Utility for sending Custom WFFM email

Email Utility for sending Custom WFFM email

In my last blog we have created a custom save action for sending the email based on the drop list selection now we will extend our last blog by creating an email utility for sending the email with all the WFFM form fields and the selected email item.

In our last blog we were calling

EmailUtility.SendCustomEmail(this, emailItem, adaptedFields);

In this blog we are creating the definition for this function.

We have created a class EmailUtility and that class has definition of above fuction.

using System;

using System.Net;

using System.Net.Mail;

using System.Text;

using System.Xml;

using Sitecore.Data.Items;

using Sitecore.StringExtensions;

using Sitecore.WFFM.Abstractions.Actions;

using Sitecore.WFFM.Actions.Base;

 

namespace NameSpace

{

public static class EmailUtility

{

public static void SendCustomEmail(WffmSaveAction saveAction, Item emailItem, AdaptedResultList adaptedFields)

{

//get the smtp host, username, password etc. from custom save action’s parameter

Item customSaveActionItem = Sitecore.Context.Database.GetItem(saveAction.ActionID);

 

string smtpHost = string.Empty;

string smtpPort = string.Empty;

string smtpUsername = string.Empty;

string smtpPassword = string.Empty;

bool IsBodyHtml = false;

bool enableSSL = false;

 

if (customSaveActionItem != null)

{

//parameters is a non-structured XML.  Add xml tags so that it becomes a xml string that can be loaded in the xmldocument object.

string xmlString = “<xml>” + customSaveActionItem.Fields[“Parameters”].Value + “</xml>”;

XmlDocument xdoc = new XmlDocument();

xdoc.LoadXml(xmlString);

 

XmlNodeList node = xdoc.GetElementsByTagName(“Host”);

if (node.Count > 0)

{

smtpHost = node[0].InnerText;

}

 

node = xdoc.GetElementsByTagName(“IsBodyHtml”);

if (node.Count > 0)

{

IsBodyHtml = (node[0].InnerText.ToLower() == “true”);

}

 

node = xdoc.GetElementsByTagName(“Login”);

if (node.Count > 0)

{

smtpUsername = node[0].InnerText;

}

 

 

node = xdoc.GetElementsByTagName(“Password”);

if (node.Count > 0)

{

smtpPassword = node[0].InnerText;

}

 

node = xdoc.GetElementsByTagName(“Port”);

if (node.Count > 0)

{

smtpPort = node[0].InnerText;

}

 

node = xdoc.GetElementsByTagName(“enableSsl”);

if (node.Count > 0)

{

enableSSL = (node[0].InnerText.ToLower() == “true”);

}

 

}

 

#region mail body

 

StringBuilder mailBody = new StringBuilder();

foreach (AdaptedControlResult adaptedControlResult in adaptedFields)

{

//one of the field in this list is the “DRIVER FIELD”.

//it’s value will be equal to GUID.  We do not want to GUID to go out in the email.

//we should replace it with a friendly version instead.

string captchaField = null;

if (!adaptedControlResult.FieldID.IsNullOrEmpty())

{

var wffmField = Sitecore.Context.Database.GetItem(adaptedControlResult.FieldID);

if (wffmField != null && wffmField.Fields[“Field Link”] != null)

{

captchaField = wffmField.Fields[“Field Link”].Value;

}

}

//Ignoring the captch field fom email body

if (captchaField == “{7FB270BE-FEFC-49C3-8CB4-947878C099E5}”) continue;

if (IsBodyHtml) mailBody.Append(“<b>”);

mailBody.Append(adaptedControlResult.FieldName);

if (IsBodyHtml) mailBody.Append(“</b>”);

 

mailBody.Append(“:”);

mailBody.Append(adaptedControlResult.Value);

 

mailBody.Append(NewLine(IsBodyHtml));

}

 

 

if (IsBodyHtml) mailBody.Append(“<b>”);

mailBody.Append(“Metadata: “);

if (IsBodyHtml) mailBody.Append(“</b>”);

mailBody.Append(NewLine(IsBodyHtml));

 

 

//add current page to mail

if (IsBodyHtml) mailBody.Append(“<b>”);

mailBody.Append(“Current Page: “);

if (IsBodyHtml) mailBody.Append(“</b>”);

mailBody.Append(NewLine(IsBodyHtml));

 

 

#endregion

 

 

 

SmtpClient smtpClient = new SmtpClient();

smtpClient.EnableSsl = enableSSL;

smtpClient.Host = smtpHost;

smtpClient.Port = string.IsNullOrEmpty(smtpPort) ? 25 : Convert.ToInt32(smtpPort);

 

 

if (!string.IsNullOrEmpty(smtpUsername) && !string.IsNullOrEmpty(smtpPassword))

{

NetworkCredential networkCredential = new NetworkCredential(smtpUsername, smtpPassword);

smtpClient.UseDefaultCredentials = false;

smtpClient.Credentials = (ICredentialsByHost)networkCredential;

}

 

MailMessage message = new MailMessage();

 

string[] toAddress = emailItem.Fields[“To_Mails_IDs”].Value.Split(new char[] { ‘,’, ‘;’ });

foreach (string address in toAddress)

{

if (!string.IsNullOrEmpty(address)) message.To.Add(new MailAddress(address));

}

 

string[] ccAddress = emailItem.Fields[“CC_Mails_IDs”].Value.Split(new char[] { ‘,’, ‘;’ });

foreach (string address in ccAddress)

{

if (!string.IsNullOrEmpty(address)) message.CC.Add(new MailAddress(address));

}

 

string[] bccAddress = emailItem.Fields[“BCC_Mails_IDs”].Value.Split(new char[] { ‘,’, ‘;’ });

foreach (string address in bccAddress)

{

if (!string.IsNullOrEmpty(address)) message.Bcc.Add(new MailAddress(address));

}

 

message.From = string.IsNullOrEmpty(emailItem.Fields[“From_Mail_ID”].Value) ? new MailAddress(“example@chs.org”) : new MailAddress(emailItem.Fields[“From_Mail_ID”].Value);

 

 

message.Subject = !string.IsNullOrEmpty(emailItem.Fields[“Mail_Subject”].Value) ? emailItem.Fields[“Mail_Subject”].Value : “Some default value”;

message.Body = mailBody.ToString();

message.IsBodyHtml = IsBodyHtml;

message.BodyEncoding = Encoding.UTF8;

message.SubjectEncoding = Encoding.UTF8;

 

smtpClient.Send(message);

}

 

private static string NewLine(bool isbodyHtml)

{

if (isbodyHtml) return “</br>”;

else return Environment.NewLine;

 

}

}

}

Advertisements

One thought on “Email Utility for sending Custom WFFM email

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s