using Sitecore.ExperienceForms.Models;
using Sitecore.ExperienceForms.Mvc.Models.Fields;
using Sitecore.ExperienceForms.Mvc.Models.SubmitActions;
using Sitecore.ExperienceForms.Processing;
using Sitecore.Reflection;
using Sitecore.Services.Core.ComponentModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using KAO.Foundation.Email;
using System.IO;
using System.Text;
namespace KAO.Foundation.ExperienceForms.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++)
{
mailAddressCollection.Add(array[i]);
}
}
protected override bool Execute(SendEmailData data, FormSubmitContext formSubmitContext)
{
bool flag;
using (MailMessage mailMessage = new MailMessage())
{
try
{
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<Foundation.Email.EmailService.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;
var attach = new Email.EmailService.Attachment
{
Filename = uploadFile.FileName,
Base64FileContents = ConvertToBase64(uploadFile.InputStream),
};
attachments.Add(attach);
}
}
}
}
}
#endregion
KAO.Foundation.Email.Email.SendEmailWithAttachments(mailMessage.From.ToString(), mailMessage.To.ToString(), mailMessage.CC.ToString(), mailMessage.Bcc.ToString(), mailMessage.Subject, mailMessage.Body, attachments.ToArray(), true);
flag = true;
}
catch (Exception exception1)
{
Exception exception = exception1;
this.Logger.LogError(exception.Message, exception, this);
flag = false;
}
}
return flag;
}
private static string ConvertToBase64(Stream stream)
{
if (stream is MemoryStream memoryStream)
{
return Convert.ToBase64String(memoryStream.ToArray());
}
var bytes = new Byte[(int)stream.Length];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(bytes, 0, (int)stream.Length);
return Convert.ToBase64String(bytes);
}
}
}