kjac / FormEditor

A form builder editor for Umbraco 7 - let your editors build forms easily with this free package.
MIT License
99 stars 42 forks source link

Setting the email entered in the form as from email for notification #146

Closed mikaelekstrom closed 7 years ago

mikaelekstrom commented 7 years ago

Is it possible to set an email address entered in the form as the from email for the notification email? So you can then respond directly to the the person submitting the forms email whitout having to log in to umbraco och create a new email?

KR, Mikael

kjac commented 7 years ago

Hi @mikaelekstrom,

Not out of the box. If you're into C#, you can hook into the BeforeSendMail event - see this section of the docs.

mikaelekstrom commented 7 years ago

Cant really find where in the code the from address is set and the email is actually sent.

kjac commented 7 years ago

@mikaelekstrom try something like this (disclaimer: I haven't tested it):

using System.Linq;
using System.Net.Mail;
using FormEditor;
using FormEditor.Events;
using FormEditor.Fields;
using Umbraco.Core;

namespace MySite
{
    public class ApplicationEvents : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            FormModel.BeforeSendMail += FormModelOnBeforeSendMail;
        }

        private void FormModelOnBeforeSendMail(FormModel sender, FormEditorMailCancelEventArgs formEditorMailCancelEventArgs)
        {
            // find the first email field and grab the submitted email address
            var from = sender
                .AllFields()
                .OfType<EmailField>()
                .FirstOrDefault(f => f.HasSubmittedValue)
                ?.SubmittedValue;

            if(string.IsNullOrWhiteSpace(from))
            {
                // no submitted email address
                return;
            }

            // set the from address on the email
            formEditorMailCancelEventArgs.MailMessage.From = new MailAddress(from);
        }
    }
}
kjac commented 7 years ago

Closing this due to lack of activity.