Customize the Send Email Message save action on Sitecore9

The Send Email Message save action sends an e-mail message every time a visitor clicks the Submit button on a WFFM form.

In this post, I’m trying to address the changes from Sitecore 8 to 9. I recently migrated a Sitecore 8 application to Sitecore 9 where WFFM 8 installed so faced a few issues in making the custom email functionality working on save action.

Code snippet on Sitecore 8 solution was as follows.

public class SendEmail : Sitecore.Form.Submit.ISaveAction
{
public void Execute(ID formId, AdaptedResultList fields, params object[] data)
{

}

}

which will not work once you migrate to WFFM 9.

To make it work we have to inherit from WffmSaveAction which is part   Sitecore.WFFM.Actions.Base namespace.

using Sitecore.Diagnostics;
using Sitecore.WFFM.Abstractions.Actions;
using Sitecore.WFFM.Actions.Base;

public class SendEmail : WffmSaveAction
{

public override void Execute(ID formId, AdaptedResultList fields, ActionCallContext actionCallContext = null, params object[] data)
{

}

}

The complete code is given below. Hope it will help someone.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.WFFM.Abstractions;
using Sitecore.WFFM.Abstractions.Actions;

using Sitecore.WFFM.Actions.Base;
namespace WFFM.Core.Extensions.SaveActions
{
public class SendEmail : WffmSaveAction
{

public override void Execute(ID formId, AdaptedResultList fields, ActionCallContext actionCallContext = null, params object[] data)
{
Item emailTemplate = Sitecore.Context.Database.SelectSingleItem(string.Format(“/sitecore/content/Site Settings/Email Templates/*[@@templateid='{0}’ and @{1}='{2}’]”, Ids.Forms.Templates.StandardEmail, FieldNames.Forms.EmailTemplates.StandardEmail.Form, formId));

if (emailTemplate == null)
throw new ArgumentNullException(“Email Template”, “Email template for the form does not exist”);

//setup email
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

message.Subject = ReplaceVariables(emailTemplate[FieldNames.Forms.EmailTemplates.StandardEmail.Subject], fields, emailTemplate[FieldNames.Forms.EmailTemplates.StandardEmail.ImagesBaseUrl]);

message.Body = ReplaceVariables(emailTemplate[FieldNames.Forms.EmailTemplates.StandardEmail.BodyText], fields, emailTemplate[FieldNames.Forms.EmailTemplates.StandardEmail.ImagesBaseUrl]);
message.IsBodyHtml = true;

message.From = new System.Net.Mail.MailAddress(emailTemplate[FieldNames.Forms.EmailTemplates.StandardEmail.From]);

string emailAddresses = ProcessAddresses(ReplaceVariables(emailTemplate[FieldNames.Forms.EmailTemplates.StandardEmail.To], fields, null));
message.To.Add(emailAddresses);

if (!string.IsNullOrEmpty(emailTemplate[FieldNames.Forms.EmailTemplates.StandardEmail.CC]))
{
emailAddresses = ProcessAddresses(ReplaceVariables(emailTemplate[FieldNames.Forms.EmailTemplates.StandardEmail.CC], fields, null));
if (!string.IsNullOrWhiteSpace(emailAddresses))
message.CC.Add(emailAddresses);
}

if (!string.IsNullOrEmpty(emailTemplate[FieldNames.Forms.EmailTemplates.StandardEmail.BCC]))
{
emailAddresses = ProcessAddresses(ReplaceVariables(emailTemplate[FieldNames.Forms.EmailTemplates.StandardEmail.BCC], fields, null));
if (!string.IsNullOrWhiteSpace(emailAddresses))
message.Bcc.Add(emailAddresses);
}

try
{
IncludeAttachments(message, fields);
}
catch (Exception ex)
{
Log.Error(“Error adding attachments to email”, ex, GetType());
}

Sitecore.MainUtil.SendMail(message);
}

private void IncludeAttachments(MailMessage mail, AdaptedResultList fields)
{
foreach (AdaptedControlResult adaptedControlResult in fields)
{
if (!string.IsNullOrEmpty(adaptedControlResult.Parameters) && adaptedControlResult.Parameters.StartsWith(“medialink”) && !string.IsNullOrEmpty(adaptedControlResult.Value))
{
ItemUri uri = ItemUri.Parse(adaptedControlResult.Value);
if (uri != (ItemUri)null)
{
Item innerItem = Database.GetItem(uri);
if (innerItem != null)
{
MediaItem mediaItem = new MediaItem(innerItem);
mail.Attachments.Add(new Attachment(mediaItem.GetMediaStream(), string.Join(“.”, mediaItem.Name, mediaItem.Extension), mediaItem.MimeType));
}
}
}
}
}

private static string ReplaceVariables(string input, AdaptedResultList fields, string imagesBaseUrl)
{
string output = input;

output = output.Replace(“${email.ImagesBaseUrl}”, imagesBaseUrl);

if (fields != null)
{
foreach (AdaptedControlResult aResult in fields)
{
output = output.Replace(string.Format(“${{enquiry.{0}}}”, aResult.FieldName), aResult.Value.Replace(“<item>”, “”).Replace(“</item>”, “, “));
}
}

return output;
}

private static string ProcessAddresses(string input)
{
if (string.IsNullOrWhiteSpace(input))
return input;

string output = input.Trim();

output = output.Replace(“,,”, “”);

if (output.EndsWith(“,”))
output = output.Remove(output.Length – 1);

return output;
}
}
}

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