Try our conversational search powered by Generative AI!

XForm MVC Custom Email

Vote:
 

Hi,

Using Episerver 8 MVC how do you create a custom email and stop the original email from being sent out preventing duplications?

ThANKS

Jon

#131750
Aug 04, 2015 18:08
Vote:
 

Hopefully the next snippets can help..

When using the xforms designer on you send button you can choose to not send the email, but only "save to the database". 

Then render your form like this:

@Html.PropertyXForm("Form", new XFormParameters() { SuccessAction = "Success", PostAction = "DoSubmit" })

And then add something like this to your controller:

[AcceptVerbs(HttpVerbs.Post)]
public virtual ActionResult Success(BasePageData currentPage, XFormPostedData xFormpostedData)
{
    //do your email stuff here.. and get your values

    var data = new XFormPageHelper().GetXFormData(this, xFormpostedData);

    foreach(var reference in xFormpostedData.XFormFragments.Select(x => x.Reference))
    {
        var value = data.GetValue(reference);
        // yada yada yada
    }

    //Return to the right page..
    if(xFormpostedData.XForm.PageGuidAfterPost != Guid.Empty)
    {
        return Redirect(UrlResolver.GetUrl(ContentRepository.Get<BasePageData>(xFormpostedData.XForm.PageGuidAfterPost).ContentLink));
    }

    //default, return to index.
    return View(CreateViewModel(currentPage));
}

[AcceptVerbs(HttpVerbs.Post)]
public virtual ActionResult DoSubmit(BasePageData currentPage, XFormPostedData xFormpostedData)
{
    if (this.ModelState.IsValid && this.ProcessXFormAction(xFormpostedData))
        return Success(currentPage, xFormpostedData);
            
    return EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<XFormPageUnknownActionHandler>().HandleAction(this);
}

protected bool ProcessXFormAction(XFormPostedData post)
{
    var xformData = new XFormPageHelper().GetXFormData(this, post);
    try
    {
        return XFormActionHelper.DoAction(xformData, post, true);
    }
    catch (Exception ex)
    {
        this.ModelState.AddModelError("ProcessXFormAction", ex.Message);
        return false;
    }
}
#131756
Aug 04, 2015 21:47
Vote:
 

Hi,

THis makes sense, thanks

Do you know if it is possible to add to the ChannelOptions Enum - somehow override the XForms library so we can create a new dropdown option the Submit option?

#131768
Aug 05, 2015 9:47
Vote:
 

No, but playing around with classes and override standard xform behaviour could help. To implement your own xform rendering add files to your "~\Views\Shared\XFormElements\" folder. For example the inputfragment.cshtml is for textboxes. The select1tasdropdownlistfragment.cshtml is for dropdowns. I'am currently writing a post about how to add a file upload field to your xforms, this will be online in the next week and maybe could help you into the right direction, keep an eye on my blog (www.codevelo.us) for that.

#131770
Aug 05, 2015 10:08
Vote:
 

Hi,

Thanks for your quick reply :).

The dropdown that we would like to add to is the Result from sending in the XForms Button Properties in the CMS where you have the Options to Save to Database, Send by Email, Send email and save to DB and CustomUrl etc. It would be great if we could Send via Custom Email as I know our client would also want to save to database from time to time.

Thanks again

#131771
Aug 05, 2015 10:13
Vote:
 

Ohw, I didn't understand that correctly. As far as I know it's not possible to extend that dropdown. But maybe someone else does.

Personally: I always choose to save into the database (for backup reasons), for me it's never one over the other, it's both or just save into the database.

#131772
Aug 05, 2015 10:16
Vote:
 

Hi Jon,

Finally cracked it. This code should suppress the default email send option - so you can create your own email in the formcontroller.

[InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class XFormsInitialization : IInitializableModule
    {
        private bool _eventsAttached = false;

        #region IInitializableModule Members

        public void Initialize(EPiServer.Framework.Initialization.InitializationEngine context)
        {
            if (!_eventsAttached)
            {
                XFormActionHelper.BeforeSubmitPostedData += XFormActionHelper_BeforeSubmitPostedData;
                _eventsAttached = true;
            }
        }

        void XFormActionHelper_BeforeSubmitPostedData(object sender, XFormDataEventArgs e)
        {

            if ((e.FormData.ChannelOptions & ChannelOptions.Email) == ChannelOptions.Email)
            {
                // Remove the send mail option
                e.FormData.ChannelOptions &= ~ChannelOptions.Email;
            }

        }

        public void Preload(string[] parameters)
        {

        }

        public void Uninitialize(EPiServer.Framework.Initialization.InitializationEngine context)
        {
            if (_eventsAttached)
            {
                XFormActionHelper.BeforeSubmitPostedData -= XFormActionHelper_BeforeSubmitPostedData;
                _eventsAttached = false;
            }
        }

        #endregion

    }
#131773
Edited, Aug 05, 2015 10:31
Vote:
 

Hi Paul,

That is perfect - thanks for everyones help,

Jon

#131774
Aug 05, 2015 10:31
Vote:
 

Hi,

Sorry to ask another question - when submitting a form via our custom email the selected values from a dropdown are Empty. Input and check boxes work well but Drop downs do not.

Im Using:

string frmName = "";
string frmValue = "";

if (fragment.GetType() == typeof(Select1tAsDropdownListFragment))
                        {
                            frmName = "<strong>" + fragment.Title + ":</strong> ";

                            Select1tAsDropdownListFragment fragmentObject = (Select1tAsDropdownListFragment)fragment;
                            IList<SelectOption> options = fragmentObject.Options;

                            StringBuilder optVal = new StringBuilder();

                            foreach (SelectOption option in options)
                            {
                                if (option.SelectedItem)
                                {
                                    optVal.Append(option.Value + ", ");
                                }
                            }

                            string keys = "";

                            foreach (var d in ViewData)
                            {
                                //keys += d.Key;

                                if (d.Key == fragmentObject.Name)
                                {
                                    keys = d.Key + " - " + d.Value;
                                }
                            }

                            frmValue = keys;

}

Do you think the values are re-binding on the post back?

#131785
Aug 05, 2015 11:18
Vote:
 

Try adding:-

frmValue = fragment.Value;
#131788
Aug 05, 2015 11:27
Vote:
 

Thanks again for all your help - this worked perfectly - I seem to have put in way too much complication in my thinking :)

#131789
Aug 05, 2015 11:29
Vote:
 

No worries. Happens to the best of us. :)

#131790
Aug 05, 2015 11:31
Vote:
 

Hi, again... still on the subject of Xforms - 

Im using the following:

 [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult XFormPost(XFormPostedData data, string contentId = "")
        {
   string MailTo = data.XForm.MailTo;
   string MailSubject = data.XForm.MailSubject;
}

and both MailTo and MailSubject are empty even though the fields are filled in in the CMS. If I use this:

 string MailTo = data.XForm.MailTo != null && data.XForm.MailTo != "" ? data.XForm.MailTo : data.SelectedSubmit.Receiver;

                    string MailSubject = data.XForm.MailSubject != null && data.XForm.MailSubject != "" ? data.XForm.MailSubject : data.SelectedSubmit.Subject;

then the MailTo is populated but the Subject seems to be auto generated and says something like: Form data from "MY FORM NAME" (NAME OF SITE) as this seems to be coming from SelectedSubmit.

Jon

#131807
Aug 05, 2015 17:41
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.