Sending a form via email to a dynamically selected email address. Is this possible?
2 posts by 2 authors in: Forums > CMS Builder
Last Post: October 28, 2015 (RSS)
By JeffC - October 28, 2015
I would like to send the contents of a form to a dynamically selected email address.
Below is what I have so far. The website visitor selects North or South from a dropdown and this is sent to sales@domain.co.uk.
I would to expand this setup so that the email addresses change dynamically. So if the website visitor selects 'North' the email will be sent to north@domain.co.uk AND sales@domain.co.uk. If they select 'South' it will be sent to south@domain.co.uk AND sales@domain.co.uk.
<div class="form-group">
<label class="control-label" for="area">Are you on the North or South of the river</label>
<select class="form-control" name="area">
<option selected="selected">Please select one</option>
<option value="North" <?php selectedIf(@$_REQUEST['North'], 'North') ?>>North</option>
<option value="South" <?php selectedIf(@$_REQUEST['South'], 'South') ?>>South</option>
</select>
</div>
</div>
// send email alert
$to="sales@domain.co.uk";
$subject="A new form has been added to the database";
if(@$_REQUEST['info_request']){$_REQUEST['info_request']=1;}else{$_REQUEST['info_request']=0;}
__TEXT__;
$from="FROM: form@domain.co.uk";
mail($to,$subject,$message,$from);
Area: {$_REQUEST['area']}
// display thanks message and clear form
$successAlerts = "Thanks, we have received your form.";
$_REQUEST = array();
}
}
By Dave - October 28, 2015
Hi Jeff,
Here's some tips. In your select options, check the name of the select field you've created:
<option value="North" <?php selectedIf(@$_REQUEST['area'], 'North') ?>>North</option>
<option value="South" <?php selectedIf(@$_REQUEST['area'], 'South') ?>>South</option>
And in your email code, test for each possible value and have the script die on invalid input (so people can't trick it sending spam to unintended emails):
// send email alert
if (@$_REQUEST['area'] == 'North') { $to = "north@domain.co.uk, sales@domain.co.uk"; }
elseif (@$_REQUEST['area'] == 'South') { $to = "south@domain.co.uk, sales@domain.co.uk"; }
else { die("Invalid area selected '" .htmlencode(@$_REQUEST['area']). "'!"); }
And if you want to use CMSB's mailing function instead of PHP's you can that function and example code for it in /lib/common.php
$errors = sendMessage(array(
'from' => "from@example.com",
'to' => "to@example.com",
'subject' => "Enter subject here, supports utf-8 content",
'text' => "Text message content",
));
if ($errors) { die($errors); }
Hope that helps!
interactivetools.com