FormBuilder - set the destination e-mail with a variable

By sending the Form results to a UDT we can pass a var along with the results to dynamically insert the destination.

Goal: Set the destination address for formbuilder by passing it in a variable.
Issue: The destination can't be set by "Passing Default Values to Forms" (Cfr. module help)

Instead of creating 12 forms for the different users I wanted to use 1 single form and set the destination by using a variable.
The first part of this article is nothing more than a "step by step" rewrite of this forum thread by eoto88 & calguy1000.
In the second part we'll add a little extra anti-spam-harvester-protection by encrypting the e-mail address we are passing along in the hidden field to stop it from showing up in the page source.

1. Set up a new form in FormBuilder

- There is no need to define a "*Email Results to set Address(es)"
- Add all the other fields you want to use
- Add a "-Hidden Field" called "hidden destination" with a bogus value. (won't be used if you make sure to pass a variable to the form containing an e-mail address...)

2. Create a UDT "Contactform"

In the UDT below, you will need to change the following values:

- if($form_id == 4) >> change the value for $form_id (you can find the correct ID at the end of your URL when editing the form. Look for "&m1_form_id=4")
- change "fld_30" / "$message" to reflect the field ID and name of your fields. (Repeat the block of code to reflect all the fields you have added to your form)

In the example below, I just repeated the block of code once for the hidden destination field and once for the message field.
You can find the various "fld_..." values in use by selecting the "Submission Template" tab when editing the form.

$actparams = $smarty->get_template_vars('actionparams');
$form_id = $actparams['form_id'];

if($form_id == 4) {
$mail = cms_utils::get_module('CMSMailer');

if(!empty($params['fld_30'])) {
$message = $params['fld_30'];
} else {
$message = '';
}

if(!empty($params['fld_33'])) {
$destination = $params['fld_33'];
} else {
$destination = '';
}

$subject = 'Contact form message';
$body = 'Message: ' . $message . 'Destination: ' . $destination . ' ';
$mail->AddAddress( $destination );
$mail->IsHTML( true );
$mail->SetBody( $body );
$mail->SetSubject( $subject );
$mail->Send();
$mail->reset();
}

3. Send the form results to the UDT

Add "*Call A User Defined Tag With the Form Results" to your form and select the UDT you just created.

4. Test the current setup

You can test the current setup by calling the form in one of your pages and passing an e-mail address to the hidden destination field.

{FormBuilder form='formname' value_fld33="$the_var_containing_the_email_address"}

You will find that with the current setup, the destination address is visible in the source of your website.
As we don't want the e-mail addresses to be up for grabs by spam harvesters, we should really encrypt this!

5. Hide the destination e-mail address in the source

First, we will need to create a new UDT, let's call it "encrypt"

$smarty = cmsms()->GetSmarty();
$key = 'CMSMS-RULEZ';
$string = $params['text'];
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
$smarty->assign('encrypted', $encrypted);

Note: Don't forget to change the value for $key!
The "encrypt" UDT will take the "text" parameter and output the contents in a var called "encrypted"... (off course, you can also use this for purposes outside of the scope of this article)

Now that the UDT is created, we can encrypt the variable we are passing to FormBuilder.
In the page or template where you want to call the form, use the encrypt UDT to encrypt the var before sending it to the form.

- Change $item->email_address to the variable containing your destination e-mail address.
- Change "formname" to the name of your form :^p
- Change value_fld33 to match the field ID of your hidden field

{encrypt text="'$item->email_address'"}
{FormBuilder form='formname' value_fld33="$encrypted"}
Now all that is left is to unencrypt the e-mail address in the UDT sending the mail! - Replace the previous block of code for your hidden destination field with the updated example below. (fld_33) - If you changed the value for $key... please also change it here... - Update the changed variable names in $body & $mail->AddAddress
$actparams = $smarty->get_template_vars('actionparams');
$form_id = $actparams['form_id'];

if($form_id == 4) {
$mail = cms_utils::get_module('CMSMailer');

if(!empty($params['fld_30'])) {
$message = $params['fld_30'];
} else {
$message = '';
}

if(!empty($params['fld_33'])) {
$key = 'CMSMS-RULEZ';
$encrypted_destination = $params['fld_33'];
$decrypted_destination = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encrypted_destination), MCRYPT_MODE_CBC, md5(md5($key))), "\0");

} else {
$decrypted_destination = '';
}

$subject = 'Contact form message';
$body = 'Message: ' . $message . 'Destination: ' . $decrypted_destination . ' ';
$mail->AddAddress( $decrypted_destination );
$mail->IsHTML( true );
$mail->SetBody( $body );
$mail->SetSubject( $subject );
$mail->Send();
$mail->reset();
}

6. Sit back and relax

All done, you should now be able to safely use one form for multiple destinations.

Comments