Episerver forms (Send email after submission)

Vote:
 

Hi!

I'am trying to create a default value for my email template for all episerver forms. I'am talking about the window where you decide if you are going to send a mail after the form is submitted.

Where if "From" is empty i want to insert a default value. I have tried doing this by jacking into episerver form events 

  • FormsSubmissionFinalized
  • FormsSubmitting

This is my initializationmodule:

[InitializableModule]
public class EpiserverFormsInitializationModule : IInitializableModule
{

public void Initialize(InitializationEngine context)
{
var formsEvents = ServiceLocator.Current.GetInstance();
formsEvents.FormsSubmissionFinalized += SubmissionFinalized;
}

public void Uninitialize(InitializationEngine context)
{
}

public void SubmissionFinalized(object o, FormsEventArgs e)
{
}


}

However in the param "e" in my SubmissionFinalized method i can find all kind of properties regarding the form but not anything for the recipient or sender to modify. Any information you can provide on how to best do this would be helpful :) 

I have tried finding a good post on how to find these values but ended up empty handed.

*Help me obi wan, you are my only hope*

Best regards Jonas.C

#146780
Mar 23, 2016 17:02
Vote:
 

The information is set on the form on the settings tab and is later used in the actor that sends emails called SendEmailAfterSubmissionActor. I can't see a way to get to that information using the above events so I'll do it another way. 

So to set a default value for the from-value you could either hook into when form is saved (the emailsettings are stored on the form content type) or add some logic to when the actor is run. I chose the latter of the two.

The actor is executed by a class called ActorsExecutingService so I'll just modify slightly what this class is doing when it's loading the actors that should be run. 

public class ImprovedActorsExecutingService : ActorsExecutingService
    {
        public const string DefaultEmail = "info@mogul.com";
        public override IEnumerable<IPostSubmissionActor> GetFormSubmissionActors(Submission submission, FormContainerBlock formContainer, FormIdentity formIden,
            HttpRequestBase request, Guid permanentFormSubmissionId, bool isFormFinalizedSubmission)
        {
            var actors = base.GetFormSubmissionActors(submission, formContainer, formIden, request, permanentFormSubmissionId, isFormFinalizedSubmission);
            var emailActor = actors.OfType<SendEmailAfterSubmissionActor>().FirstOrDefault();
           
                var emailModels = emailActor.Model as IEnumerable<EmailTemplateActorModel>;
                foreach (var emailModel in emailModels)
                {
                    if (string.IsNullOrWhiteSpace(emailModel.FromEmail))
                    {
                        emailModel.FromEmail = DefaultEmail;
                    }
                }
            return actors;
        }
    }

Of course you also need to tell EPiServer to use your new improved version :)

You can do this when configuring the ioc container

   private static void ConfigureContainer(ConfigurationExpression container)
        {
            container.For<ActorsExecutingService>().Use<ImprovedActorsExecutingService>();
...
}

There is probably an easier way to do this...or there should be at least but it's a fun way of doing it :)

#146788
Edited, Mar 24, 2016 0:25
Mark Stott - Nov 14, 2022 10:32
This solution has helped me greatly, thank you for sharing.
Vote:
 

Thank you for your help Daniel! :) It worked out with the solution you provided.

#146844
Mar 29, 2016 10:40
Vote:
 

Sweet! I didn't try setting the from address in smtp tag in web.config I just realized. That would of course be easiest if it worked. Probably should be implemented that way if not... :)
But it was a fun coding exercise anyway...

#146861
Mar 29, 2016 11:23
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.