From 1ae7ca32f8cebf19ca7529e70973dbcd635f4c41 Mon Sep 17 00:00:00 2001 From: Kenn Jacobsen Date: Thu, 8 Dec 2016 11:14:15 +0100 Subject: [PATCH] Add BeforeSendMail event --- Docs/extend.md | 3 +++ .../Events/FormEditorMailCancelEventArgs.cs | 20 +++++++++++++++ .../FormEditorMailCancelEventHandler.cs | 4 +++ Source/Solution/FormEditor/FormEditor.csproj | 2 ++ Source/Solution/FormEditor/FormModel.cs | 25 +++++++++++++++++-- 5 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 Source/Solution/FormEditor/Events/FormEditorMailCancelEventArgs.cs create mode 100644 Source/Solution/FormEditor/Events/FormEditorMailCancelEventHandler.cs diff --git a/Docs/extend.md b/Docs/extend.md index 4f46930..a9c5f37 100644 --- a/Docs/extend.md +++ b/Docs/extend.md @@ -11,6 +11,9 @@ If you need to react to form submissions (e.g. to create your own workflow), you There's an example of how to use the form submission events in the [samples](../Samples/Event handling/) section. +## Email sending events +Before any form submission emails are sent (notification and confirmation emails), the `BeforeSendMail` event is invoked so you can manipulate the `MailMessage` as you please. This event is cancelable. If it is cancelled, the email will not be sent. + ## Creating a custom condition for cross field validation The cross field validation system can be extended with your own custom conditions. Read more [here](extend_condition.md). diff --git a/Source/Solution/FormEditor/Events/FormEditorMailCancelEventArgs.cs b/Source/Solution/FormEditor/Events/FormEditorMailCancelEventArgs.cs new file mode 100644 index 0000000..ff27f40 --- /dev/null +++ b/Source/Solution/FormEditor/Events/FormEditorMailCancelEventArgs.cs @@ -0,0 +1,20 @@ +using System.ComponentModel; +using System.Net.Mail; + +namespace FormEditor.Events +{ + public class FormEditorMailCancelEventArgs : CancelEventArgs + { + public FormEditorMailCancelEventArgs(MailMessage mailMessage, string emailType) + { + MailMessage = mailMessage; + EmailType = emailType; + } + + // the mail that's being sent + public MailMessage MailMessage { get; private set; } + + // the type of mail - "confirmation" or "notification" + public string EmailType { get; private set; } + } +} \ No newline at end of file diff --git a/Source/Solution/FormEditor/Events/FormEditorMailCancelEventHandler.cs b/Source/Solution/FormEditor/Events/FormEditorMailCancelEventHandler.cs new file mode 100644 index 0000000..38bfa16 --- /dev/null +++ b/Source/Solution/FormEditor/Events/FormEditorMailCancelEventHandler.cs @@ -0,0 +1,4 @@ +namespace FormEditor.Events +{ + public delegate void FormEditorMailCancelEventHandler(FormModel sender, FormEditorMailCancelEventArgs e); +} diff --git a/Source/Solution/FormEditor/FormEditor.csproj b/Source/Solution/FormEditor/FormEditor.csproj index a7b21cb..51f0f51 100644 --- a/Source/Solution/FormEditor/FormEditor.csproj +++ b/Source/Solution/FormEditor/FormEditor.csproj @@ -263,6 +263,8 @@ + + diff --git a/Source/Solution/FormEditor/FormModel.cs b/Source/Solution/FormEditor/FormModel.cs index b18937e..6e0e5b4 100644 --- a/Source/Solution/FormEditor/FormModel.cs +++ b/Source/Solution/FormEditor/FormModel.cs @@ -90,6 +90,7 @@ public IEnumerable Rows // events public static event FormEditorCancelEventHandler BeforeAddToIndex; public static event FormEditorEventHandler AfterAddToIndex; + public static event FormEditorMailCancelEventHandler BeforeSendMail; public bool CollectSubmittedValues(bool redirect = true) { @@ -649,10 +650,10 @@ private void SendEmailType(string subject, string senderAddress, string recipien subject = InterpolateSubmittedValues(subject); // send emails to the recipients - SendEmails(subject, emailBody, senderEmailAddress, addresses, uploadedFiles); + SendEmails(subject, emailBody, senderEmailAddress, addresses, uploadedFiles, emailType); } - private static void SendEmails(string subject, string body, MailAddress from, IEnumerable to, HttpPostedFile[] uploadedFiles) + private void SendEmails(string subject, string body, MailAddress from, IEnumerable to, HttpPostedFile[] uploadedFiles, string emailType) { if(string.IsNullOrEmpty(body)) { @@ -688,6 +689,26 @@ private static void SendEmails(string subject, string body, MailAddress from, IE attachments.Add(attachment); } + // #23 - raise an event before sending the mail + if(BeforeSendMail != null) + { + try + { + var cancelEventArgs = new FormEditorMailCancelEventArgs(mail, emailType.ToLowerInvariant()); + BeforeSendMail.Invoke(this, cancelEventArgs); + if(cancelEventArgs.Cancel) + { + Log.Info("The {0} email was not sent because an event handler for BeforeSendMail cancelled the mail delivery.", emailType); + return; + } + } + catch (Exception ex) + { + // an event handler failed - log error and continue + Log.Error(ex, "An event handler for BeforeSendMail threw an exception."); + } + } + // send the mail as fire-and-forget (pass the attachments as state) ThreadPool.QueueUserWorkItem(state => {