Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mikaelekstrom opened this issue Sep 5, 2017 · 4 comments
Closed

Comments

@mikaelekstrom
Copy link

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
Copy link
Owner

kjac commented Sep 5, 2017

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
Copy link
Author

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

@kjac
Copy link
Owner

kjac commented Sep 5, 2017

@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
Copy link
Owner

kjac commented Sep 21, 2017

Closing this due to lack of activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants