jools619 Posted October 17, 2010 Share Posted October 17, 2010 Hi Apologies as this may seem like a trival matter, but im new to coding and just need a bit of help.... I have a form which includes a drop down menu of various departments. Depending on which department is chosen, the form is then sent to different people. This is the html code for the drop down: <select name="department" size="1" id="department"> <option value="" selected>Please select</option> <option value="Health & Safety">Health & Safety</option> <option value="Property">Property</option> </select> So for example the first option mails out to [email protected] [email protected] and the second would go to [email protected] mr@[email protected] and [email protected] etc... I have a separate php file which gathers the variables so i was thinking an if statement would need to be put into this page? Out of interest i tried something myself and sort of got a result im after: <select name='department' id='department'> <option value='[email protected]'>Department 1</option> <option value='[email protected]'>Department 1</option> </select> Then i used $email_to = $_POST['department']; in the php file. But i would like to be able to send to 2 or 3 people so tried: <select name='department' id='department'> <option value='[email protected], [email protected] '>Department 1</option> </select> This didnt work Any help very much appreciated Thanks Jools Quote Link to comment https://forums.phpfreaks.com/topic/216077-need-help-with-sending-to-multiple-email-addresses/ Share on other sites More sharing options...
Pikachu2000 Posted October 17, 2010 Share Posted October 17, 2010 It "didn't work" in what way? Quote Link to comment https://forums.phpfreaks.com/topic/216077-need-help-with-sending-to-multiple-email-addresses/#findComment-1122975 Share on other sites More sharing options...
phpfreak Posted October 17, 2010 Share Posted October 17, 2010 I would strongly advise you against putting e-mail addresses in your forms. Spam Bots and spiders are going to find this, and also someone can override these e-mail addresses via an external form and post to your processing page and end up spamming as many people as they want. I recommend you create an option and in your processing code - not visible to the public - based on which option you choose which e-mail (hard coded, or through a database of e-mails) etc. Quote Link to comment https://forums.phpfreaks.com/topic/216077-need-help-with-sending-to-multiple-email-addresses/#findComment-1122989 Share on other sites More sharing options...
jools619 Posted October 17, 2010 Author Share Posted October 17, 2010 Pikachu2000 - well it works when i used: <select name='department' id='department'> <option value='[email protected]'>Department 1</option> </select> But when i tried to add in another email address like: <select name='department' id='department'> <option value='[email protected], [email protected]'>Department 1</option> </select> The email form wouldnt send. I thought by putting in another email address, i could send to another person, but i assume the syntax in the option value is wrong? PHP Freak - Yes very true on the spamming aspect, it was usuing this method just to get a basic model working. Although i found this out on the web which was reccommended but no luck: <?php function SendMail( $email_to ) { $personalinjuryEmailArray = array( "[email protected]", "[email protected]" ); $credithireEmailArray = array( "[email protected]" ); switch( $_POST['department'] ){ case "1": foreach ( $personalinjuryEmailArray as $email_to ){ SendMail( $email_to ); } break; case "2": foreach ( $credithireEmailArray as $email_to ){ SendMail( $email_to ); } break; } $email = strip_tags($_POST['email']); $message = "Title: " . strip_tags($_POST['title']) . "\r\n"; $message .= "Name: " . strip_tags($_POST['name']) . "\r\n"; $message .= "Email Address: " . strip_tags($_POST['email']) . "\r\n"; $message .= "Address: " . strip_tags($_POST['address']) . "\r\n"; $message .= "Telephone: " . strip_tags($_POST['telephone']) . "\r\n"; $message .= "Message: " . strip_tags($_POST['comment']) . "\r\n"; $headers = "From: $email\r\n"; $headers .= "Reply-To: $email\r\n"; $subject = "Website Contact Form Enquiry". $subject; if(mail($email_to, $subject, $message, $headers)){ echo 'sent'; } else echo 'failed'; } ?> Is there anyway the first option could work with multiple addresses? Thanks for the replies though guys Quote Link to comment https://forums.phpfreaks.com/topic/216077-need-help-with-sending-to-multiple-email-addresses/#findComment-1123000 Share on other sites More sharing options...
phpfreak Posted October 20, 2010 Share Posted October 20, 2010 Pikachu2000 - well it works when i used: <select name='department' id='department'> <option value='[email protected]'>Department 1</option> </select> But when i tried to add in another email address like: <select name='department' id='department'> <option value='[email protected], [email protected]'>Department 1</option> </select> The email form wouldnt send. I thought by putting in another email address, i could send to another person, but i assume the syntax in the option value is wrong? PHP Freak - Yes very true on the spamming aspect, it was usuing this method just to get a basic model working. Although i found this out on the web which was reccommended but no luck: <?php function SendMail( $email_to ) { $personalinjuryEmailArray = array( "[email protected]", "[email protected]" ); $credithireEmailArray = array( "[email protected]" ); switch( $_POST['department'] ){ case "1": foreach ( $personalinjuryEmailArray as $email_to ){ SendMail( $email_to ); } break; case "2": foreach ( $credithireEmailArray as $email_to ){ SendMail( $email_to ); } break; } $email = strip_tags($_POST['email']); $message = "Title: " . strip_tags($_POST['title']) . "\r\n"; $message .= "Name: " . strip_tags($_POST['name']) . "\r\n"; $message .= "Email Address: " . strip_tags($_POST['email']) . "\r\n"; $message .= "Address: " . strip_tags($_POST['address']) . "\r\n"; $message .= "Telephone: " . strip_tags($_POST['telephone']) . "\r\n"; $message .= "Message: " . strip_tags($_POST['comment']) . "\r\n"; $headers = "From: $email\r\n"; $headers .= "Reply-To: $email\r\n"; $subject = "Website Contact Form Enquiry". $subject; if(mail($email_to, $subject, $message, $headers)){ echo 'sent'; } else echo 'failed'; } ?> Is there anyway the first option could work with multiple addresses? Thanks for the replies though guys You can send to multiple addresses via the mail function just comma separate the addresses in the TO argument - but the issue is unless you setup a BCC header - all the e-mail recipients would see each other. I'm not sure if you would want to do that or not... perhaps just do a loop and send one e-mail through the function per e-mail address. Quote Link to comment https://forums.phpfreaks.com/topic/216077-need-help-with-sending-to-multiple-email-addresses/#findComment-1124595 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.