PHP FORM: Multiple Recipients based on Drop Down Selection

I need a form to send to multiple different receipients based off the user dropdown selection. Here is what I’ve read up on so far… I can get it to say success but I dont recieve the email. Please help!!

Html:

Gmail yahoo

PHP:

<?php $i = $_POST["sendto"]; switch ($i) { case "gmail": $sendto = "[email protected]"; break; case "recpro": $sendto = "[email protected]"; break; default: $sendto = "[email protected]"; //opional break; } $subject = "test email"; function sanitize( $s ){ $injections = array('/(\n+)/i', '/(\r+)/i', '/(\t+)/i', '/(%0A+)/i', '/(%0D+)/i', '/(%08+)/i', '/(%09+)/i' ); $s = preg_replace( $injections, '', $s ); return $s; } //catch the posted data $first_name = sanitize( $_POST['first_name'] ); $last_name = sanitize( $_POST['last_name'] ); $email = sanitize( $_POST['email'] ); $telephone = sanitize( $_POST['telelphone'] ); $body = $telephone."\n\n"; $body.= $first_name."<$email>"; $headers = "From: $last_name<$email>"; if(mail($send_to, $subject, $body, $headers)): echo "success", $sent_to, $subject, $body, $headers; else: echo "error"; endif; ?>

I need it to be header injection safe.

So there are a number of things wrong with this script. First the option tag cannot have an id or a name attribute. You can see the valid attributes here http://www.w3schools.com/tags/tag_option.asp

The sanitizing you are doing isn’t great. I would suggest just running them through trim() and htmlentities() instead. I have a tutorial on this kind of stuff http://amecms.com/article/How-to-validate-a-form-the-right-way.

You need to wrap the processing code in an if() so that it only runs if the form has been posted. So something like this usually.
[php]
if(isset($_POST[‘submit’])) // This assumes your submit button has a name attribute of “submit”.
{
// Your processing code here.
}
[/php]
Now the main reason it’s not working is cause you are defining $sendto in the switch but then trying to use $send_to in the mail() AND THEN you’re using $sent_to below that which doesn’t exist either. You really need to turn on error reporting if it’s not already showing errors for you. You would have seen all the wrong $send_to stuff if it was turned on. You can turn it on with these lines of code at the very top of your script.

[php]
if(mail($send_to, $subject, $body, $headers)):
echo “success”, $sent_to, $subject, $body, $headers;
[/php]
Should be
[php]
if(mail($sendto, $subject, $body, $headers)):
echo “success”, $sendto, $subject, $body, $headers;
[/php]
[php]
ini_set(‘display_errors’,1);
error_reporting(E_ALL);
[/php]

Lastly you can read a in depth article I wrote on php email here http://amecms.com/article/Sending-email-in-PHP-and-reaching-the-inbox

Sponsor our Newsletter | Privacy Policy | Terms of Service