Chronos21 Posted March 28, 2014 Share Posted March 28, 2014 I'm trying to make a webpage for a friend of a friend who is trying to sell his domain (for an outrages amount of money, but hey, his money is as green as anyone's). I have the website designed and with some generous help from the internet have written a PHP script that should send the contents of the form on the page to my email(temporarily). Every time I test it out, though, I get the confirmation page that says the script ran, but no email shows up. Like I said, I'm new at this. I've coded before, but never in PHP. Are there any REALLY common mistakes that people make that could account for this? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted March 28, 2014 Share Posted March 28, 2014 there are probably around a dozen different things that could prevent any particular code from functioning, while giving an indication that it completed. to narrow down the possibilities, you would need to post your code. Quote Link to comment Share on other sites More sharing options...
Chronos21 Posted March 28, 2014 Author Share Posted March 28, 2014 Ah, I'm sorry. Here it is. <?php if(isset($_POST['email'])) { $email_to = "myemail@gmail.com"; $email_subject = "offer"; function died($error) { echo "We are sorry, but there were error(s) found with the form you submitted. "; echo "These errors appear below.<br /><br />"; echo $error."<br /><br />"; echo "Please go back and fix these errors.<br /><br />"; die(); } if(!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['email']) || !isset($_POST['telephone']) || !isset($_POST['comments'])) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $first_name = $_POST['first_name']; // required $last_name = $_POST['last_name']; // required $email_from = $_POST['email']; // required $telephone = $_POST['telephone']; $comments = $_POST['comments']; // required $error_message = ""; $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(!preg_match($email_exp,$email_from)) { $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; } $string_exp = "/^[A-Za-z .'-]+$/"; if(!preg_match($string_exp,$first_name)) { $error_message .= 'The First Name you entered does not appear to be valid.<br />'; } if(!preg_match($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.<br />'; } if(strlen($comments) < 2) { $error_message .= 'The Comments you entered do not appear to be valid.<br />'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "First Name: ".clean_string($first_name)."\n"; $email_message .= "Last Name: ".clean_string($last_name)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Telephone: ".clean_string($telephone)."\n"; $email_message .= "Comments: ".clean_string($comments)."\n"; $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); ?> Thank you for contacting us. We will be in touch with you very soon. <?php } ?> Quote Link to comment Share on other sites More sharing options...
paddy_fields Posted March 28, 2014 Share Posted March 28, 2014 Remove the @ sign before 'mail' and it will hopefully show you a helpful error message as to why it isn't sending. @ ignores errors messages (http://www.php.net/manual/en/language.operators.errorcontrol.php) Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted March 28, 2014 Share Posted March 28, 2014 (edited) the code you are attempting to use contains three problems that are either preventing it from working, preventing it from telling you why it isn't working, or is telling you it worked when it may not have. 1) the @ error suppressor in front of the mail() function call is preventing the reporting of any errors detected by php or returned to php from the sending mail server. remove the @ and temporarily, for debugging purposes, add the following two lines of code immediately after the first opening <?php tag - ini_set("display_errors", "1"); error_reporting(-1); report back with any error messages you get that you cannot solve. 2) the mail() function returns a true/false value that you need to test in your code. if it returns a true value, it means that the sending mail server accepted the email and will likely try to send it to the to: email address. you should only display the success message if the mail function returned a true value. this does not mean that the sending mail server will succeed in sending the email or that the receiving mail server accepted the email. it only means that php found a sending mail server at your web hosting and that mail server didn't return any error to php. 3) the email is not from the email address that the visitor entered in the form. the email is being sent from the sending mail server at your web hosting to the to: email address. the from address you put into the mail header should be a real mail box at your sending mail server at your web hosting or at least the domain name in the from address must correspond to your web hosting. most receiving mail servers currently test that the domain the mail says it is from can be traced back to the actual sending mail server. in your testing if you entered a from address at @gmail and you are using a to: address @gmail, unless your web hosting is at google, the receiving mail server knows you didn't send this email from a gmail mail server and will discard it. Edited March 28, 2014 by mac_gyver Quote Link to comment 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.