sturobinson81 Posted August 22, 2012 Share Posted August 22, 2012 i have some php code that manages an array of email recipients for use in a drop down menu on a form. this part on its own works fine, but when the form is sent, i'd like a copy to be emailed to a permanent email address aswell as the selected email address in the array. I assumed it was the following line of code i should be editing... but being a php newbie i can't figure out how to alter it. :: Original line :: $my_email = $recipients[$_REQUEST['recipient']]; :: My guess at editing that does not work :: $my_email = "srobinson@myserver.co.uk",$recipients[$_REQUEST['recipient']]; am i a million miles off? thanks. Stu Quote Link to comment https://forums.phpfreaks.com/topic/267412-email-form/ Share on other sites More sharing options...
Adam Posted August 22, 2012 Share Posted August 22, 2012 You need to add an additional BCC header; take a look at the fourth mail parameter. Please post the whole code if you need any more help, what you posted previously is just a variable declaration. Quote Link to comment https://forums.phpfreaks.com/topic/267412-email-form/#findComment-1371359 Share on other sites More sharing options...
sturobinson81 Posted August 22, 2012 Author Share Posted August 22, 2012 Hi Adam, Thanks for your help... although I'm not sure how to use it Here's the code <?php error_reporting(E_ALL ^ E_NOTICE); $my_email = "srobinson@myserver.co.uk"; /* Optional. Enter a From: email address. Only do this if you know you need to. By default, the email you get from the script will show the visitor's email address as the From: address. In most cases this is desirable. On the majority of setups this won't be a problem but a minority of hosts insist that the From: address must be from a domain on the server. For example, if you have the domain example.com hosted on your server, then the From: email address must be something@example.com (See your host for confirmation). This means that your visitor's email address will not show as the From: address, and if you hit "Reply" to the email from the script, you will not be replying to your visitor. You can get around this by hard-coding a From: address into the script using the configuration option below. Enabling this option means that the visitor's email address goes into a Reply-To: header, which means you can hit "Reply" to respond to the visitor in the conventional way. (You can also use this option if your form does not collect an email address from the visitor, such as a survey, for example, and a From: address is required by your email server.) The default value is: $from_email = ""; Enter the desired email address between the quotes, like this example: $from_email = "contact@example.com"; In these cases, it is not uncommon for the From: ($from_email) address to be the same as the To: ($my_email) address, which on the face of it appears somewhat goofy, but that's what some hosts require. */ $from_email = ""; /* Optional. Enter the continue link to offer the user after the form is sent. If you do not change this, your visitor will be given a continue link to your homepage. If you do change it, remove the "/" symbol below and replace with the name of the page to link to, eg: "mypage.htm" or "http://www.elsewhere.com/page.htm" */ $continue = "/"; /* Step 3: Save this file (FormToEmail.php) and upload it together with your webpage containing the form to your webspace. IMPORTANT - The file name is case sensitive! You must save it exactly as it is named above! THAT'S IT, FINISHED! You do not need to make any changes below this line. */ $errors = array(); // Remove $_COOKIE elements from $_REQUEST. if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}} // Validate email field. if(isset($_REQUEST['email']) && !empty($_REQUEST['email'])) { $_REQUEST['email'] = trim($_REQUEST['email']); if(substr_count($_REQUEST['email'],"@") != 1 || stristr($_REQUEST['email']," ") || stristr($_REQUEST['email'],"\\") || stristr($_REQUEST['email'],":")){$errors[] = "Email address is invalid";}else{$exploded_email = explode("@",$_REQUEST['email']);if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){$errors[] = "Email address is invalid";}else{if(substr_count($exploded_email[1],".") == 0){$errors[] = "Email address is invalid";}else{$exploded_domain = explode(".",$exploded_email[1]);if(in_array("",$exploded_domain)){$errors[] = "Email address is invalid";}else{foreach($exploded_domain as $value){if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){$errors[] = "Email address is invalid"; break;}}}}}} } // Check referrer is from same site. if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){$errors[] = "You must enable referrer logging to use the form";} // Check for a blank form. function recursive_array_check_blank($element_value) { global $set; if(!is_array($element_value)){if(!empty($element_value)){$set = 1;}} else { foreach($element_value as $value){if($set){break;} recursive_array_check_blank($value);} } } recursive_array_check_blank($_REQUEST); if(!$set){$errors[] = "You cannot send a blank form";} unset($set); // Display any errors and exit if errors exist. if(count($errors)){foreach($errors as $value){print "$value<br>";} exit;} if(!defined("PHP_EOL")){define("PHP_EOL", strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" : "\n");} // Build message. function build_message($request_input){if(!isset($message_output)){$message_output ="";}if(!is_array($request_input)){$message_output = $request_input;}else{foreach($request_input as $key => $value){if(!empty($value)){if(!is_numeric($key)){$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");} $message = build_message($_REQUEST); $message = $message . PHP_EOL.PHP_EOL."-- ".PHP_EOL."Thank you for using FormToEmail"; $message = stripslashes($message); $subject = "FormToEmail Comments"; $subject = stripslashes($subject); if($from_email) { $headers = "From: " . $from_email; $headers .= PHP_EOL; $headers .= "Reply-To: " . $_REQUEST['email']; } else { $from_name = ""; if(isset($_REQUEST['name']) && !empty($_REQUEST['name'])){$from_name = stripslashes($_REQUEST['name']);} $headers = "From: {$from_name} <{$_REQUEST['email']}>"; } $recipients = array( 'recipient_1' => 'customerservices@example.com', 'recipient_2' => 'customerservices@example.com', 'recipient_3' => 'customerservices@example.com', 'recipient_4' => 'customerservices@example.com', 'recipient_5' => 'customerservices@example.com', 'recipient_6' => 'customerservices@example.com', 'recipient_7' => 'customerservices@example.com', 'recipient_8' => 'customerservices@example.com', 'recipient_9' => 'customerservices@example.com', 'recipient_10' => 'customerservices@example.com', 'recipient_11' => 'customerservices@example.com', 'recipient_12' => 'customerservices@example.com', 'recipient_13' => 'customerservices@example.com', ); $my_email = $recipients[$_REQUEST['recipient']]; mail($my_email,$subject,$message,$headers); ?> Quote Link to comment https://forums.phpfreaks.com/topic/267412-email-form/#findComment-1371362 Share on other sites More sharing options...
Adam Posted August 22, 2012 Share Posted August 22, 2012 Okay, you just need to add an additional header after these: $headers = "From: " . $from_email; $headers .= PHP_EOL; $headers .= "Reply-To: " . $_REQUEST['email']; Instead of "Reply-To" or "From" though, you need to use "Bcc". Don't forget to split the headers up with a line-break, although I wouldn't recommend using PHP_EOL like you have now. I would use "\r\n". Quote Link to comment https://forums.phpfreaks.com/topic/267412-email-form/#findComment-1371364 Share on other sites More sharing options...
sturobinson81 Posted August 22, 2012 Author Share Posted August 22, 2012 Cheers Adam, I've added $headers .= "BCC: me@myserver.co.uk\r\n"; but when i've tested it i'm not getting mail to the address in the header. Do i need to add/change anything else? Quote Link to comment https://forums.phpfreaks.com/topic/267412-email-form/#findComment-1371367 Share on other sites More sharing options...
Adam Posted August 22, 2012 Share Posted August 22, 2012 Headers are 1 per line, so you need a line-break after the previous header, otherwise the string will contain: Reply-To: <some email here>Bcc: <some email here> Quote Link to comment https://forums.phpfreaks.com/topic/267412-email-form/#findComment-1371369 Share on other sites More sharing options...
sturobinson81 Posted August 22, 2012 Author Share Posted August 22, 2012 Thanks Adam, I think I'm probably missing/overlooking something... I've tried these 3 different options, the only one that runs my php script completely is option 3, but i don't get any mail to the bcc address. 1----------------- $headers = "From: " . $from_email"\r\n"; $headers .= "Reply-To: " . $_REQUEST['email']"\r\n"; $headers .= "BCC: me@myserver"; 2------------------ $headers = "From: " . $from_email; $headers .= "\r\n" $headers .= "Reply-To: " . $_REQUEST['email']; $headers .= "\r\n" $headers .= "BCC: me@myserver.co.uk"; 3---------------- $headers = "From: " . $from_email; $headers .= PHP_EOL; $headers .= "Reply-To: " . $_REQUEST['email']\r\n"; $headers .= PHP_EOL; $headers .= "BCC: me@myserver.co.uk"; Quote Link to comment https://forums.phpfreaks.com/topic/267412-email-form/#findComment-1371374 Share on other sites More sharing options...
Adam Posted August 22, 2012 Share Posted August 22, 2012 "BCC" should be "Bcc" - headers are case-sensitive. Sorry didn't spot that last time. Quote Link to comment https://forums.phpfreaks.com/topic/267412-email-form/#findComment-1371376 Share on other sites More sharing options...
Adam Posted August 22, 2012 Share Posted August 22, 2012 #2 is the best way in terms of line-breaks by the way. The problem with PHP_EOL is that it uses the convention of the server PHP's installed on. Meaning if it's a unix/linux based OS it will be "\n", but for Windows it will be "\r\n". Using just "\n" could break the email if it's received on a Windows server. "\r\n" will work on both however, so using it just ensures the headers will always be readable. Quote Link to comment https://forums.phpfreaks.com/topic/267412-email-form/#findComment-1371380 Share on other sites More sharing options...
sturobinson81 Posted August 22, 2012 Author Share Posted August 22, 2012 I've tried the following, but still don't seem to be getting the mail to the bcc address $headers = "From: " . $from_email; $headers .= "\r\n"; $headers .= "Reply-To: " . $_REQUEST['email']; $headers .= "\r\n"; $headers .= "Bcc: me@myserver.co.uk"; Quote Link to comment https://forums.phpfreaks.com/topic/267412-email-form/#findComment-1371382 Share on other sites More sharing options...
Adam Posted August 22, 2012 Share Posted August 22, 2012 Just before the mail call, could you do a var_dump of $headers and post us what's shown in the source? Quote Link to comment https://forums.phpfreaks.com/topic/267412-email-form/#findComment-1371387 Share on other sites More sharing options...
sturobinson81 Posted August 22, 2012 Author Share Posted August 22, 2012 I placed the following just before the mail($my_email..... etc line var_dump($headers); and got string(9) "From: <>" Quote Link to comment https://forums.phpfreaks.com/topic/267412-email-form/#findComment-1371393 Share on other sites More sharing options...
Adam Posted August 22, 2012 Share Posted August 22, 2012 Ah, apologies there's an IF statement in your code that checks for $from_name, and only if present defines those headers. Otherwise it defines a different header (which also looks to have problems). I would move the Bcc header to just before the mail() call, so that it will always be present. Quote Link to comment https://forums.phpfreaks.com/topic/267412-email-form/#findComment-1371394 Share on other sites More sharing options...
sturobinson81 Posted August 22, 2012 Author Share Posted August 22, 2012 I'll give it a go and let you know... I was going to try and add the address to each address in the array and see if that worked e.g. 'recipient_2' => 'me@myserver.com','me2@myserver.com', Quote Link to comment https://forums.phpfreaks.com/topic/267412-email-form/#findComment-1371396 Share on other sites More sharing options...
Adam Posted August 22, 2012 Share Posted August 22, 2012 No I wouldn't do that, the 'hidden' email address will be visible to the recipient. Quote Link to comment https://forums.phpfreaks.com/topic/267412-email-form/#findComment-1371398 Share on other sites More sharing options...
sturobinson81 Posted August 22, 2012 Author Share Posted August 22, 2012 I didnt get the mail through this time either... I have noticed the following line appears as the sender on the emails i do get to the addresses from the array "Bcc:artwork"@myserver.co.uk Quote Link to comment https://forums.phpfreaks.com/topic/267412-email-form/#findComment-1371411 Share on other sites More sharing options...
Adam Posted August 22, 2012 Share Posted August 22, 2012 That will be because of the broken "From" header I suspect. I placed the following just before the mail($my_email..... etc line var_dump($headers); and got string(9) "From: <>" Quote Link to comment https://forums.phpfreaks.com/topic/267412-email-form/#findComment-1371412 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.