Creating custom submit action to send the emails in Sitecore Forms

This a common ask if you are not using EXM, you need to send thank you emails or emails to the admin team with the details of the submitted entry.

This can be achieved in 2 steps:

  1. Create a class which inherits from Sitecore.ExperienceForms.Mvc.Processing.SubmitActions.SendEmail and write your custom logic.
  2. Add a Custom Submit Action under /sitecore/system/Settings/Forms/Submit Actions
  1. Adding a class file which looks similar to below code:
using Sitecore.ExperienceForms.Models;
using Sitecore.ExperienceForms.Mvc.Models.Fields;
using Sitecore.ExperienceForms.Mvc.Models.SubmitActions;
using Sitecore.ExperienceForms.Processing;
using Sitecore.Reflection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;

namespace Sitecore.Foundation.SitecoreExtensions.Extensions
    public class CustomSendEmail : Sitecore.ExperienceForms.Mvc.Processing.SubmitActions.SendEmail
        public CustomSendEmail(ISubmitActionData submitActionData) : base(submitActionData)

        private static string GetFieldValue(IViewModel postedField)
            if (postedField.GetType().GetMethod("GetStringValue") != null)
                return ReflectionUtil.CallMethod(postedField, "GetStringValue").ToString();

            return "";

        private static string ReplaceFieldValues(string content, FormSubmitContext formSubmitContext)
            string str = content;
            if (string.IsNullOrEmpty(str))
                return string.Empty;
            foreach (IViewModel field in formSubmitContext.Fields)
                str = str.Replace(string.Format("{0}{1}{2}", '[', field.Name, ']'), GetFieldValue(field));
            return str;

        private static void AddRecipients(MailAddressCollection mailAddressCollection, string mailAddresses)
            string[] array = (
                from p in mailAddresses.Split(new char[] { ';' })
                select p.Trim() into p
                where !string.IsNullOrEmpty(p)
                select p).ToArray<string>();
            for (int i = 0; i < (int)array.Length; i++)

        protected override bool Execute(SendEmailData data, FormSubmitContext formSubmitContext)
            bool flag;
            using (MailMessage mailMessage = new MailMessage())
                    mailMessage.From = new MailAddress(ReplaceFieldValues(data.From, formSubmitContext));
                    mailMessage.Subject = ReplaceFieldValues(data.Subject, formSubmitContext);
                    mailMessage.Body = ReplaceFieldValues(data.Body, formSubmitContext);
                    mailMessage.IsBodyHtml = true;
                    AddRecipients(mailMessage.To, ReplaceFieldValues(data.To, formSubmitContext));
                    AddRecipients(mailMessage.CC, ReplaceFieldValues(data.Cc, formSubmitContext));
                    AddRecipients(mailMessage.Bcc, ReplaceFieldValues(data.Bcc, formSubmitContext));
                    #region Attachment 
                    var attachments = new List<Attachment>();
                    var uploadFieldList = formSubmitContext.Fields
                          .Where(f => f.GetType() == typeof(FileUploadViewModel));
                    if (uploadFieldList != null && uploadFieldList.Count() > 0)
                        foreach (var uploadField in uploadFieldList)
                            if (uploadField != null)
                                var uploadFieldVM = (FileUploadViewModel)uploadField;
                                if (uploadFieldVM.Files != null && uploadFieldVM.Files.Count > 0)
                                    for (var i = 0; i < uploadFieldVM.Files.Count; i++)
                                        var uploadFile = uploadFieldVM.Files[i];
                                        uploadFile.InputStream.Position = 0;
                                        mailMessage.Attachments.Add(new Attachment(
                    flag = true;
                catch (Exception exception1)
                    Exception exception = exception1;
                    this.Logger.LogError(exception.Message, exception, this);
                    flag = false;
            return flag;

2. Adding the custom submit action.

a. Go to /sitecore/system/Settings/Forms/Submit Actions
b. Insert Submit Action

c. Name it as “Custom Send Email”.
d. Add the Model Type based on where you have created the class and select Editor as SendEmail

Publish the changes and you are ready to use this custom submit action.


Thank you… Keep Learning… Keep Sitecoring.. 🙂

Leave a Reply

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

You are commenting using your 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