Created
February 17, 2022 02:23
-
-
Save klawingco/82a29f6c72933427d615e2d38a84315f to your computer and use it in GitHub Desktop.
Custom SFE SendEmail Submit Action
This file contains hidden or 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 Sitecore.Diagnostics; | |
| using Sitecore.ExperienceForms.Models; | |
| using Sitecore.ExperienceForms.Mvc.Models.SubmitActions; | |
| using Sitecore.ExperienceForms.Mvc.Processing.SubmitActions; | |
| using Sitecore.ExperienceForms.Processing; | |
| using Sitecore.Reflection; | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Net.Mail; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| namespace KL.Extensions.SubmitActions | |
| { | |
| public class CustomSendEmail : SendEmail | |
| { | |
| public CustomSendEmail(ISubmitActionData submitActionData) : base(submitActionData) | |
| { | |
| } | |
| protected override bool Execute(SendEmailData data, FormSubmitContext formSubmitContext) | |
| { | |
| Assert.ArgumentNotNull((object)formSubmitContext, nameof(formSubmitContext)); | |
| using (MailMessage mailMessage = new MailMessage()) | |
| { | |
| try | |
| { | |
| CustomSendEmail.SetMessageAttributes(mailMessage, data, formSubmitContext); | |
| this.MailSender.SendMail(mailMessage); | |
| return true; | |
| } | |
| catch (Exception ex) | |
| { | |
| this.Logger.LogError(ex.Message, ex, (object)this); | |
| return false; | |
| } | |
| } | |
| } | |
| private static void SetMessageAttributes( | |
| MailMessage message, | |
| SendEmailData data, | |
| FormSubmitContext formSubmitContext) | |
| { | |
| Assert.ArgumentNotNull((object)message, nameof(message)); | |
| message.From = new MailAddress(CustomSendEmail.ReplaceFieldValues(data.From, formSubmitContext)); | |
| message.Subject = CustomSendEmail.ReplaceFieldValues(data.Subject, formSubmitContext); | |
| message.Body = CustomSendEmail.ReplaceFieldValues(data.Body, formSubmitContext); | |
| message.IsBodyHtml = true; | |
| CustomSendEmail.AddRecipients(message.To, CustomSendEmail.ReplaceFieldValues(data.To, formSubmitContext)); | |
| CustomSendEmail.AddRecipients(message.CC, CustomSendEmail.ReplaceFieldValues(data.Cc, formSubmitContext)); | |
| CustomSendEmail.AddRecipients(message.Bcc, CustomSendEmail.ReplaceFieldValues(data.Bcc, formSubmitContext)); | |
| } | |
| private static string ReplaceFieldValues(string content, FormSubmitContext formSubmitContext) | |
| { | |
| string str = content; | |
| if (string.IsNullOrEmpty(str)) | |
| return string.Empty; | |
| foreach (IViewModel field in (IEnumerable<IViewModel>)formSubmitContext.Fields) | |
| str = str.Replace(string.Format("{0}{1}{2}", (object)'[', (object)field.Name, (object)']'), CustomSendEmail.GetFieldValue(field)); | |
| return str; | |
| } | |
| private static void AddRecipients( | |
| MailAddressCollection mailAddressCollection, | |
| string mailAddresses) | |
| { | |
| string str = mailAddresses; | |
| char[] chArray = new char[1] { ';' }; | |
| foreach (string addresses in ((IEnumerable<string>)str.Split(chArray)).Select<string, string>((Func<string, string>)(p => p.Trim())).Where<string>((Func<string, bool>)(p => !string.IsNullOrEmpty(p))).ToArray<string>()) | |
| mailAddressCollection.Add(addresses); | |
| } | |
| private static string GetFieldValue(IViewModel postedField) | |
| { | |
| Assert.ArgumentNotNull((object)postedField, nameof(postedField)); | |
| try | |
| { | |
| return ReflectionUtil.CallMethod((object)postedField, "GetStringValue").ToString(); | |
| } | |
| catch(InvalidOperationException) | |
| { | |
| return String.Empty; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment