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 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!

Dave Edis - Senior Developer
interactivetools.com