Developer Forum »
Contact form - send confirmation email conditionally
36 posts

Hi devs!

I have a contact form defined in "Forms" module, which I'd like to send a confirmation email (to the person who posts the form) but only in case this person checks a checkbox. The checkbox is a field of the contact form. The email field of the form has a setting "Send confirmation to this email" which can be set on/off and which works for me, but I'd like it to be a user decision, not system-wide. Is there any way I can do it?

120 posts

Hi, to allow for this you cannot use the built in feature "Send confirmation to this email" as it is not possible to "hook into" and customize this internal event. However, what you can do instead is to override event methods on the form class, and send a customized email here instead. In this method you could add whatever logic you want, like only sending an email if a certain checkbox is checked.

Create a new class call MyWebForm (Choose other name if you want), let it inherit from the built in class "WebForm". Then override the method: OnSubmit(WebFormSubmit submit) and choose "Replace Class" to make sure it replaces the internal WebForm class. (see image)

In this method you must parse the submit.SubmitData property to determine if your checkbox is checked or not. In contains an XML string where all field values are stored. Example:

<?xml version="1.0" encoding="utf-16"?>
<Submit Id="2" Guid="574f2c9c-a071-4771-9517-f91142333f39" FormId="8347" When="2013-04-09 08:38:10.109" Tics="635010934901093750">
  <From LCID="1033" UserId="-5953" SessionId="fbea03a7-a4c4-4285-882c-58a93385396a" UserAgent="Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)" UserHostAddress="89.191.9.219" UserHostName="89.191.9.219" Url="http://www.webnodes.com/Templates/AdvancedArticlePage.aspx?nid=9008" />
  <Fields>
    <Field Id="yourname" Value="fredrik" />
    <Field Id="youremail" Value="webnodes.com" />
    <Field Id="yourphone" Value="0948120984120984" />
    <Field Id="Classify your support request" Value="1" />
    <Field Id="ifyespleaseprovideuswiththenameoftheleaddeveloperfortheoriginalproject" Value="ole" />
    <Field Id="newfield6" Value="1" />
    <Field Id="newfield7" Value="" />
  </Fields>
</Submit>

 

Ok?

Attachments
36 posts

Thanks for the hint - I will try it out and let you know about my results.

36 posts

Hi Ole!

Your hint put me on the right track, I overridden the method, parsed the XML and wanted to send the email, but there is just one thing I am missing now - I can't seem to find a way to form a subject and message within the OnSubmit method. I have lot of building blocks available (email address, submit ID, email templates with [%Field: "TextArea"%] placeholders, and I could (with a lots of ugly code) turn it into a final email message, but maybe there is a way to send the copy of the email to me by simple method call?

36 posts

Hi devs! 

I managed to make this work in the following way (describing just in case someone else needs similar behavior):

- in the form create 2 fields of type "email", both hidden

- one of them is the email of the person who sends the form - this one has the "Send confirmation to this email" set to false, it is always filled in with proper email

- the other email field is a helper field, which has "Send confirmation to this email" set to true, this one is not filled in (value="")

- fill the helper field (with JavaScript, when clicking send) with the address taken from first email field, but do it only if the "send copy to me" checkbox is checked

- all built-in Webnodes logic is re-used, so I can setup the forms messages templates within webnodes, no need to create new form class and no need to override onSubmit method

Cheers

120 posts

Thanks for sharing!

1