Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Validating Email Address that sends the email after submitting a form

Vote:
 

Hello,

I have a requirement to validate the email address that is used on "Send email after submission". Only company email address can be used.

Can we use any Form API that handles the "Add" event on "Create email template" while creating/configuring a form?



Thanks

Nilakantha

#195677
Aug 03, 2018 13:15
Vote:
 

Hi Nilakantha,

There's no such event on "Create email template".

As far as I know there's only way to validate emails which are used in "Send mail after submission" section that is you have to hook into Content's Publishing Event.

My sample code

using EPiServer.Framework;
using System.Collections.Generic;
using System.Linq;
using EPiServer.Framework.Initialization;
using EPiServer.Core;
using EPiServer.ServiceLocation;
using EPiServer;
using EPiServer.Forms.Implementation.Elements;
using EPiServer.Forms.Implementation.Actors;

namespace DebugForms_9.Business.Initialization
{
	[ModuleDependency(typeof(EPiServer.Forms.EditView.InitializationModule), typeof(EPiServer.Web.InitializationModule), typeof(EPiServer.Shell.ShellInitialization))]
	public class FormsInit : IInitializableModule
	{
		private IContentEvents _contentEvents;

		public void Initialize(InitializationEngine context)
		{
			if (_contentEvents == null)
			{
				_contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
			}

			_contentEvents.PublishingContent += PublishingContent;
		}

		public void Uninitialize(InitializationEngine context)
		{
			if (_contentEvents == null)
			{
				_contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
			}
			_contentEvents.PublishingContent -= PublishingContent;
		}

		/// <summary>
		/// Hook to CMS's PublishedContent for updating structure if needed.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void PublishingContent(object sender, ContentEventArgs e)
		{
			// validate email in Actor template
			if (!(e.Content is FormContainerBlock))
			{
				return;
			}

			var emailActorModels = e.Content.Property.GetPropertyValue<IEnumerable<EmailTemplateActorModel>>("SendEmailAfterSubmissionActor");
			if (emailActorModels == null || emailActorModels.Count() == 0)
			{
				return;
			}

			foreach(var model in emailActorModels)
			{
				var fromEmail = model.FromEmail;
				if (!string.IsNullOrWhiteSpace(fromEmail) && !fromEmail.EndsWith("@episerver.com"))
				{
					e.CancelAction = true;
					e.CancelReason = "Must be an episerver email address";
				}
			}
		}
	}
}
#195706
Aug 06, 2018 8:35
Vote:
 

Thanks Quan.  Just wondering if there will be any performance implication as we are intercepting publish event of all content types (although we are then checking for forms only)

Nilakantha

#195752
Aug 07, 2018 11:22
Vote:
 

Hi Nilakantha,

In my opinion, I don't think we'll have problem with this because CMS.Core exposes events for us to do such this thing. Moreover, the logic is not complicated and does not consume much resources. 

Our addons use Core's events also.

/Quan

#195796
Edited, Aug 08, 2018 2:37
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.