Skip to content

Commit

Permalink
Fix #23 - add BeforeSendMail event
Browse files Browse the repository at this point in the history
  • Loading branch information
kjac authored Dec 9, 2016
2 parents 0664284 + 1ae7ca3 commit 58552f1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Docs/extend.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
20 changes: 20 additions & 0 deletions Source/Solution/FormEditor/Events/FormEditorMailCancelEventArgs.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace FormEditor.Events
{
public delegate void FormEditorMailCancelEventHandler(FormModel sender, FormEditorMailCancelEventArgs e);
}
2 changes: 2 additions & 0 deletions Source/Solution/FormEditor/FormEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@
<Compile Include="Events\FormEditorCancelEventHandler.cs" />
<Compile Include="Events\FormEditorEventArgs.cs" />
<Compile Include="Events\FormEditorEventHandler.cs" />
<Compile Include="Events\FormEditorMailCancelEventArgs.cs" />
<Compile Include="Events\FormEditorMailCancelEventHandler.cs" />
<Compile Include="Fields\CustomFieldFixedValues.cs" />
<Compile Include="Fields\Statistics\FieldExtensions.cs" />
<Compile Include="Fields\Statistics\IValueFrequencyStatisticsField.cs" />
Expand Down
25 changes: 23 additions & 2 deletions Source/Solution/FormEditor/FormModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public IEnumerable<Row> Rows
// events
public static event FormEditorCancelEventHandler BeforeAddToIndex;
public static event FormEditorEventHandler AfterAddToIndex;
public static event FormEditorMailCancelEventHandler BeforeSendMail;

public bool CollectSubmittedValues(bool redirect = true)
{
Expand Down Expand Up @@ -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<MailAddress> to, HttpPostedFile[] uploadedFiles)
private void SendEmails(string subject, string body, MailAddress from, IEnumerable<MailAddress> to, HttpPostedFile[] uploadedFiles, string emailType)
{
if(string.IsNullOrEmpty(body))
{
Expand Down Expand Up @@ -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 =>
{
Expand Down

0 comments on commit 58552f1

Please sign in to comment.