JulianWill Posted March 4, 2011 Share Posted March 4, 2011 Ok I am very new to php so please excuse my lack of knowledge. I have a simple contact form that I have placed onto one of my sites. The form works correctly in that it get sent to the correct address without any problems. The issue I am having is with the 'From' header. It will not show the senders email address as I would like. Instead it seems to be showing the server address. I have been 'playing' with the code for 2 days now and I still cannot get it to work. Any suggestions would be greatfully received. Regards. <?php if(!$_POST) exit; $email = $_POST['Email']; $name = $_POST['Name']; $telephone = $_POST['Telephone']; $comments = $_POST['Comments']; //$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS'; if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){ $error.="Invalid email address entered"; $errors=1; } if($errors==1) echo $error; else{ $values = array ('Name','Email','Telephone','Comments'); $required = array('Name','Email','Telephone','Comments'); $your_email = "**********************"; $email_name = "Message From your Website:"; foreach($values as $key => $value){ if(in_array($value,$required)){ if ($key != 'Name' && $key != 'Comments' && $key != 'Telephone') { if( empty($_POST[$value]) ) { echo 'Please go back and complete all fields, thank you.'; exit; } } $email_content .= $value.': '.$_POST[$value]."\n"; } } if(@mail($your_email,$email_name,$email_content)) { echo 'Thank you for your message, we will be in touch shortly'; } else { echo 'ERROR!'; } } $headers = "From: $email\r\n"."Reply-To: $email\r\n".'X-Mailer: PHP/' . phpversion(); @mail($your_email,$name,$email,$telephone,$comments,$headers); ?> Link to comment Share on other sites More sharing options...
MatthewJ Posted March 4, 2011 Share Posted March 4, 2011 @mail($your_email,$name,$email,$telephone,$comments,$headers); Have a look at the mail function in the manual. I think you're a bit confused on the parameters it can take. This is from the manual bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] ) It will most always be mail($emailtosendto, $subjectofemail, $emailmessagebody, $headers) It looks like you're just adding a comma and any field you want sent as parameters, mail() expects very specific things Link to comment Share on other sites More sharing options...
JulianWill Posted March 4, 2011 Author Share Posted March 4, 2011 Matthew Thats very helpful. I have amended the code as follows however, now I get two emails sent. One in the correct format that I required (with the email address showing) and another with the server address showing. How can I stop this second mail being sent. <?php if(!$_POST) exit; $email = $_POST['Email']; $name = $_POST['Name']; $telephone = $_POST['Telephone']; $comments = $_POST['Comments']; //$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS'; if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){ $error.="Invalid email address entered"; $errors=1; } if($errors==1) echo $error; else{ $values = array ('Name','Email','Telephone','Comments'); $required = array('Name','Email','Telephone','Comments'); $your_email = "*****"; $email_subject = "Message From the HDS Builders Website:"; foreach($values as $key => $value){ if(in_array($value,$required)){ if ($key != 'Name' && $key != 'Comments' && $key != 'Telephone') { if( empty($_POST[$value]) ) { echo 'Please go back and complete all fields, thank you.'; exit; } } $email_content .= $value.': '.$_POST[$value]."\n"; } } if(@mail($your_email,$email_name,$email_content)) { echo 'Thank you for your message, we will be in touch shortly'; } else { echo 'ERROR!'; } } $headers = "From: $email\r\n"."Reply-To: $email\r\n".'X-Mailer: PHP/' . phpversion(); mail($your_email, $email_subject, $email_content, $headers) ?> Link to comment Share on other sites More sharing options...
MatthewJ Posted March 4, 2011 Share Posted March 4, 2011 You're sending it twice here: if(@mail($your_email,$email_name,$email_content)) { echo 'Thank you for your message, we will be in touch shortly'; } else { echo 'ERROR!'; } and here: mail($your_email, $email_subject, $email_content, $headers) The first one is right above the other, and the second one is the correct one Link to comment Share on other sites More sharing options...
cyberRobot Posted March 4, 2011 Share Posted March 4, 2011 You're calling the mail() function twice: if(@mail($your_email,$email_name,$email_content)) { ... mail($your_email, $email_subject, $email_content, $headers) Link to comment Share on other sites More sharing options...
JulianWill Posted March 4, 2011 Author Share Posted March 4, 2011 Thanks for the promt reply. I have tried deleting this line: if(@mail($your_email,$email_name,$email_content, $headers)) { However when I do that I get this error message: Parse error: syntax error, unexpected T_ELSE in /home/rcttest/public_html/contact.php on line 88 Which is reffering to this line: } else { I cannot work out why this is. Regards. Link to comment Share on other sites More sharing options...
MatthewJ Posted March 4, 2011 Share Posted March 4, 2011 <?php if(!$_POST) exit; $email = $_POST['Email']; $name = $_POST['Name']; $telephone = $_POST['Telephone']; $comments = $_POST['Comments']; //$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS'; if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){ $error.="Invalid email address entered"; $errors=1; } if($errors==1) echo $error; else{ $values = array ('Name','Email','Telephone','Comments'); $required = array('Name','Email','Telephone','Comments'); $your_email = "*****"; $email_subject = "Message From the HDS Builders Website:"; foreach($values as $key => $value){ if(in_array($value,$required)){ if ($key != 'Name' && $key != 'Comments' && $key != 'Telephone') { if( empty($_POST[$value]) ) { echo 'Please go back and complete all fields, thank you.'; exit; } } $email_content .= $value.': '.$_POST[$value]."\n"; } } } $headers = "From: $email\r\n"."Reply-To: $email\r\n".'X-Mailer: PHP/' . phpversion(); mail($your_email, $email_subject, $email_content, $headers) ?> Give that a try Link to comment Share on other sites More sharing options...
JulianWill Posted March 4, 2011 Author Share Posted March 4, 2011 A quick question. Is there a way to keep the formating when I copy and paste code from the site. Regards. Link to comment Share on other sites More sharing options...
JulianWill Posted March 4, 2011 Author Share Posted March 4, 2011 Matthew Thanks for that. It has solved my problem. The only thing is now my 'Thank you' message has gone. What would be the best way to re-introduce it? Regards Julian Link to comment Share on other sites More sharing options...
cyberRobot Posted March 4, 2011 Share Posted March 4, 2011 Note that the code is missing a semi-colon; the last line should be: mail($your_email, $email_subject, $email_content, $headers); Also, the if/else version that you had before is better if you're looking to catch errors: $headers = "From: $email\r\n"."Reply-To: $email\r\n".'X-Mailer: PHP/' . phpversion(); if(@mail($your_email, $email_subject, $email_content, $headers)) { echo 'Thank you for your message, we will be in touch shortly'; } else { echo 'ERROR!'; } Link to comment Share on other sites More sharing options...
JulianWill Posted March 4, 2011 Author Share Posted March 4, 2011 Cyber Thanks for your replies. When I use the if/else version I am still getting error messages. The version that Matthew has pasted works well but without my 'Thank you' message. Do you know what would be the best way to impliment this? Regards Julian Link to comment Share on other sites More sharing options...
cyberRobot Posted March 4, 2011 Share Posted March 4, 2011 Did you update the if() part with the mail() function that contains all the arguments: if(@mail($your_email, $email_subject, $email_content, $headers)) { Link to comment Share on other sites More sharing options...
MatthewJ Posted March 4, 2011 Share Posted March 4, 2011 if(@mail($your_email, $email_subject, $email_content, $headers)) { echo 'Thank you for your message, we will be in touch shortly'; } else { echo 'ERROR!'; } Replace the mail() line with that Link to comment Share on other sites More sharing options...
JulianWill Posted March 4, 2011 Author Share Posted March 4, 2011 Ok, That woks but as a result the mail goes into the junk mail folder (it didnt bedfore). Can this be solved? regards Link to comment Share on other sites More sharing options...
cyberRobot Posted March 4, 2011 Share Posted March 4, 2011 Depending on what you use to view e-mail, there should be a way to indicate that anything from a specific e-mail address isn't junk. What do you use to view e-mail? Link to comment Share on other sites More sharing options...
Muddy_Funster Posted March 4, 2011 Share Posted March 4, 2011 ensure that your From: address is a proper and legit' email address from a recognised domain. Link to comment Share on other sites More sharing options...
JulianWill Posted March 4, 2011 Author Share Posted March 4, 2011 I am using outlook 2010. I have just sent another two and they went to the inbox so hopefully they will keep doing that. In sending those messages I have noticed another problem. If the user does not complete their email address correctly then they get taken to the error message that looks like this: Invalid email address enteredERROR! How can I change the code to get rid of the ERROR! at the end. Thanks Link to comment Share on other sites More sharing options...
cyberRobot Posted March 4, 2011 Share Posted March 4, 2011 I am using outlook 2010. I have just sent another two and they went to the inbox so hopefully they will keep doing that. This should help for managing Outlook's junk mail filter: http://office.microsoft.com/en-us/outlook-help/add-names-to-the-junk-e-mail-filter-lists-HA010355043.aspx Link to comment Share on other sites More sharing options...
JulianWill Posted March 4, 2011 Author Share Posted March 4, 2011 Cyber Thanks for the reply. Its a very useful article. Any ideas on how I can get rid of the 'ERROR'. Regards Link to comment Share on other sites More sharing options...
cyberRobot Posted March 4, 2011 Share Posted March 4, 2011 It looks like the problem is that your mail() function is outside of your if/else statement. If you move it back in there, the error text should go away. if($errors==1) echo $error; else{ ... $headers = "From: $email\r\n"."Reply-To: $email\r\n".'X-Mailer: PHP/' . phpversion(); if(@mail($your_email, $email_subject, $email_content, $headers)) { echo 'Thank you for your message, we will be in touch shortly'; } else { echo 'ERROR!'; } } ?> Link to comment Share on other sites More sharing options...
JulianWill Posted March 5, 2011 Author Share Posted March 5, 2011 Could you show me exactly where I shold be putting the mail() function. I have moved it to where I think it should go and it does solve the email error message issue however it then creates another with the 'Please go back and complete all fields, thank you' message. This message now appears like this: ERROR!Please go back and complete all fields, thank you. Thanks Link to comment Share on other sites More sharing options...
cyberRobot Posted March 7, 2011 Share Posted March 7, 2011 You'll need to add it after your foreach loop: <?php ... if($errors==1) echo $error; else{ ... foreach($values as $key => $value){ if(in_array($value,$required)){ if ($key != 'Name' && $key != 'Comments' && $key != 'Telephone') { if( empty($_POST[$value]) ) { echo 'Please go back and complete all fields, thank you.'; exit; } } $email_content .= $value.': '.$_POST[$value]."\n"; } } $headers = "From: $email\r\n"."Reply-To: $email\r\n".'X-Mailer: PHP/' . phpversion(); if(@mail($your_email, $email_subject, $email_content, $headers)) { echo 'Thank you for your message, we will be in touch shortly'; } else { echo 'ERROR!'; } } ?> Basically, you only want to send out a message if there wasn't any errors found in the foreach loop. And since the loop is set to exit if any errors are found, you shouldn't see anything after an error was found. Link to comment Share on other sites More sharing options...
JulianWill Posted March 7, 2011 Author Share Posted March 7, 2011 Thanks again for the reply. I have the code as follows: <?php if(!$_POST) exit; $email = $_POST['Email']; $name = $_POST['Name']; $telephone = $_POST['Telephone']; $comments = $_POST['Comments']; //$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS'; if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){ $error.="Invalid email address entered"; $errors=1; } if($errors==1) echo $error; else{ $values = array ('Name','Email','Telephone','Comments'); $required = array('Name','Email','Telephone','Comments'); $your_email = "*********.co.uk"; $email_subject = "Message From the Website:"; foreach($values as $key => $value){ if(in_array($value,$required)){ if ($key != 'Name' && $key != 'Comments' && $key != 'Telephone') { if( empty($_POST[$value]) ) { echo 'Please go back and complete all fields, thank you.'; exit; } } $email_content .= $value.': '.$_POST[$value]."\n"; } } } $headers = "From: $email\r\n"."Reply-To: $email\r\n".'X-Mailer: PHP/' . phpversion(); if(@mail($your_email, $email_subject, $email_content, $headers)) { echo 'Thank you for your message, we will be in touch shortly'; } else {echo 'ERROR!'; } ?> Which I thought was correct however it is still showing the work 'ERROR' with the invalid email message. Link to comment Share on other sites More sharing options...
cyberRobot Posted March 7, 2011 Share Posted March 7, 2011 It needs to go below your foreach loop, but inside the else statement which tests the validity of the e-mail: if($errors==1) echo $error; else{ ... foreach... //<-- mail() function goes here } Here is the exact code: <?php if(!$_POST) exit; $email = $_POST['Email']; $name = $_POST['Name']; $telephone = $_POST['Telephone']; $comments = $_POST['Comments']; //$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS'; if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){ $error.="Invalid email address entered"; $errors=1; } if($errors==1) echo $error; else{ $values = array ('Name','Email','Telephone','Comments'); $required = array('Name','Email','Telephone','Comments'); $your_email = "*********.co.uk"; $email_subject = "Message From the Website:"; foreach($values as $key => $value){ if(in_array($value,$required)){ if ($key != 'Name' && $key != 'Comments' && $key != 'Telephone') { if( empty($_POST[$value]) ) { echo 'Please go back and complete all fields, thank you.'; exit; } } $email_content .= $value.': '.$_POST[$value]."\n"; } } $headers = "From: $email\r\n"."Reply-To: $email\r\n".'X-Mailer: PHP/' . phpversion(); if(@mail($your_email, $email_subject, $email_content, $headers)) { echo 'Thank you for your message, we will be in touch shortly'; } else { echo 'ERROR!'; } } ?> Link to comment Share on other sites More sharing options...
JulianWill Posted March 8, 2011 Author Share Posted March 8, 2011 Cyber Its all working perfectly now. Thanks for taking the time to reply and to share your knowledge with me. I am getting there.................slowly. Regards Link to comment Share on other sites More sharing options...
Recommended Posts