Episerver Forms avoid wrong sender address when sending e-mail

Vote:
 

In Forms the editors can choose to 'Send email after form submission' and then insert a From mailaddress.

We see that this often goes wrong. An e-mail address is entered which is not supported by the mail server (not the same domain as the website itself).

what is the best way to avoid this?

Can this field be disabled; offer a dropdown with options or provide the form with a validation on this?

#274500
Feb 24, 2022 10:54
Vote:
 

Hi Robbie,

You can create a custom control for it and do the validation and all.

#274538
Feb 25, 2022 7:05
Vote:
 

It's possible, but you need to be prepared to do some customization since there is no really easy way to switch it out. 😊

First, you need to create a model that inherits the EmailTemplateActorModel and overrides the FromEmail property. You could validate it, use a selection factory to limit the choices or use a JsonIgnoreAttribute and ScaffoldColumnAttribute to hide it (this would be a bit more effort). I went with some simple validation as an example:

[Serializable]
public class ValidateFromEmailTemplateActorModel : EmailTemplateActorModel
{
	[RegularExpression("^[A-Za-z0-9._%+-]+@jakejon.es$", ErrorMessage = "The email address must be from a jakejon.es domain.")]
	public override string FromEmail { get; set; }
}

Now create a property for this, note the EditorHintAttribute which we'll reference in the next step:

[EditorHint("ValidateFromEmailTemplateActorEditor")]
[PropertyDefinitionTypePlugIn(DisplayName = "Message Template")]
public class ValidateFromPropertyEmailTemplateActorList : PropertyGenericList<ValidateFromEmailTemplateActorModel> {}

Next, we add an editor descriptor. This is exactly the same as the default Optimizely CMS one (the EmailTemplateActorCollectionEditorDescriptor) but uses our new model type (and the UI hint from above):

[EditorDescriptorRegistration(TargetType = typeof(IEnumerable<ValidateFromEmailTemplateActorModel>), UIHint = "ValidateFromEmailTemplateActorEditor")]
public class ValidateFromEmailTemplateActorCollectionEditorDescriptor : CollectionEditorDescriptor<ValidateFromEmailTemplateActorModel>
{
	public ValidateFromEmailTemplateActorCollectionEditorDescriptor()
	{
		ClientEditingClass = "epi-forms/contentediting/editors/EmailTemplateActorEditor";
	}

	public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
	{
		GridDefinition.GridSettings["richTextColumns"] = new[] { "body" };
		base.ModifyMetadata(metadata, attributes);
	}
}

Finally, we need to add a new submission actor that uses our property (and therefore new model). This can just inherit the SendEmailAfterSubmissionActor with the property type being overridden:

public class ValidateFromSendEmailAfterSubmissionActor : SendEmailAfterSubmissionActor
{
	public override Type PropertyType => typeof(ValidateFromPropertyEmailTemplateActorList);
}

If you run at this point you'll have 2 different submission actors on your Settings tab, the bottom one should apply the validation. You may need to add some translations to your language XMLs for everything to show up looking nice.

This blog post covers hiding the default send email submission actor: https://www.getadigital.com/blog/replacing-built-in-email-actor-with-custom-implementation-in-episerver-forms, it's easy enough just requires one more EditorDescriptor.

Good luck!

#274560
Feb 25, 2022 17:37
Robbie - Mar 02, 2022 13:40
thanks for the clear and detailed explanation
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.