Leko Posted July 15, 2012 Share Posted July 15, 2012 Hello, Thanks very much for taking the time to take a look at my issue. I made a simple email script to mail form data. It seems to process correctly, and the success page loads after submit, but no email is actually sent. Is there some kind of SMTP setting I need to change in order for my host server to send mail? Or is there an error in my code? ***************************************************** Contents of send_form_email.php: <?php if(isset($_POST['email'])) { // set email destination and subject line. $email_to = "[email protected]"; $email_subject = "CWCourier.com Price Quote Request"; function died($error) { // your error code can go here echo "We are very 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(); } // validation that expected data exists if(!isset($_POST['name']) || !isset($_POST['telephone']) || !isset($_POST['email']) || !isset($_POST['from_city']) || !isset($_POST['to_city']) || !isset($_POST['description']) || !isset($_POST['dimensions']) || !isset($_POST['delivery_time'])) { died('There appears to be missing information in the form you submitted. Please go back and supply all required information so that we can supply your quote as accurately as possible.'); } $name = $_POST['name']; // required $telephone = $_POST['telephone']; // required $email_from = $_POST['email']; // required $from_city = $_POST['from_city']; // required $to_city = $_POST['to_city']; // required $description = $_POST['description']; // required $dimensions = $_POST['dimensions']; // required $delivery_time = $_POST['delivery_time']; // required $flexible = $_POST['flexible']; // automatic $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,$name)) { $error_message .= 'The name you entered does not appear to be valid.<br />'; } if(!preg_match($string_exp,$from_city) || !preg_match($string_exp,$to_city)) { $error_message .= 'A city you entered does not appear to be valid.<br />'; } if(strlen($description) < 2) { $error_message .= 'The description you entered does not appear to be valid.<br />'; } if(strlen($dimensions) < 2) { $error_message .= 'The dimensions you entered do not appear to be valid.<br />'; } if(strlen($delivery_time) < 2) { $error_message .= 'The delivery time you entered does not appear to be valid.<br />'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Quote request details:\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "Name: ".clean_string($name)."\n"; $email_message .= "Phone: ".clean_string($telephone)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Pickup City: ".clean_string($from_city)."\n"; $email_message .= "Delivery City: ".clean_string($to_city)."\n"; $email_message .= "Description of Shipment: ".clean_string($description)."\n"; $email_message .= "Dimensions & Weight: ".clean_string($dimensions)."\n"; $email_message .= "Date and Time of Delivery: ".clean_string($delivery_time)."\n"; $email_message .= "Delivery Time is (Flexible or Mandatory): ".clean_string($flexible)."\n"; // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); ?> <!-- success response --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>CW Courier - Quote</title> <meta name="keywords" content="" /> <meta name="description" content="" /> <link href="default.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="header"> <h1> </h1> <h2> </h2> </div> <div id="menu"> <ul> <li><a href="index.html" accesskey="1" title="">Home</a></li> <li><a href="services.html" accesskey="2" title="">Services</a></li> <li><a href="#" accesskey="3" title="">Price Quote</a></li> <li><a href="testimonials.html" accesskey="4" title="">Testimonials</a></li> <li><a href="routes.html" accesskey="5" title="">Routes</a></li> </ul> </div> <div id="content"><div class="inner_copy"></div> <div id="colOne"> <h2>Thank you</h2> Thank you for requesting a price quote with CW Courier. We will look over your requirements immediately and get back to you. Good day! </div> <div style="clear:both;"> </div> </div> <div id="footer"> <div class="fleft"> <p>Copyright 2012, Tharsis Media. All rights reserved.</p></div><div class="fright"></div><div class="fcenter"><p> </p></div> </div> </body> </html> <?php } ?> ***************************************************** Contents of HTML page that the script is contained on: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>CW Courier - Quote</title> <meta name="keywords" content="" /> <meta name="description" content="" /> <link href="default.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="header"> <h1> </h1> <h2> </h2> </div> <div id="menu"> <ul> <li><a href="index.html" accesskey="1" title="">Home</a></li> <li><a href="services.html" accesskey="2" title="">Services</a></li> <li><a href="#" accesskey="3" title="">Price Quote</a></li> <li><a href="testimonials.html" accesskey="4" title="">Testimonials</a></li> <li><a href="routes.html" accesskey="5" title="">Routes</a></li> </ul> </div> <div id="content"><div class="inner_copy"></div> <div id="colOne"> <h2>receive price quote (all fields required)</h2> <form name="contactform" method="post" action="send_form_email.php"> <table width="450px"> <tr> <td valign="top"> <label for="name">First & Last Name *</label> </td> <td valign="top"> <input type="text" name="name" maxlength="50" size="30"> </td> </tr> <tr> <td valign="top""> <label for="telephone">Telephone Number *</label> </td> <td valign="top"> <input type="text" name="telephone" maxlength="20" size="30"> </td> </tr> <tr> <td valign="top"> <label for="email">Email Address *</label> </td> <td valign="top"> <input type="text" name="email" maxlength="50" size="30"> </td> </tr> <tr> <td valign="top"> <label for="from_city">City and State of Pickup *</label> </td> <td valign="top"> <input type="text" name="from_city" maxlength="40" size="30"> </td> </tr> <tr> <td valign="top"> <label for="to_city">City and State of Delivery *</label> </td> <td valign="top"> <input type="text" name="to_city" maxlength="40" size="30"> </td> </tr> <tr> <td valign="top"> <label for="description">Description of Shipment *</label> </td> <td valign="top"> <textarea name="description" maxlength="200" cols="24" rows="3"></textarea> </td> </tr> <tr> <td valign="top"> <label for="dimensions">Dimensions & Weight *</label> </td> <td valign="top"> <textarea name="dimensions" maxlength="200" cols="24" rows="3"></textarea> </td> </tr> <tr> <td valign="top"> <label for="delivery_time">Date and Time of Delivery *</label> </td> <td valign="top"> <input type="text" name="delivery_time" maxlength="60" size="30"> </td> </tr> <tr> <td valign="top"> <label for="flexible">Delivery Time is * </label> </td> <td valign="top"> <input type="radio" name="flexible" value="Flexible" checked>Flexible <input type="radio" name="flexible" value="Mandatory">Mandatory </td> </tr> <td colspan="2" style="text-align:center"> <input type="submit" value="Submit Quote Request"></a> </td> </tr> </table> </form> </div> <div style="clear:both;"> </div> </div> <div id="footer"> <div class="fleft"> <p>Copyright 2012, Tharsis Media. All rights reserved.</p></div><div class="fright"></div><div class="fcenter"><p> </p></div> </div> </body> </html> MOD EDIT: . . . tags added. Quote Link to comment https://forums.phpfreaks.com/topic/265723-some-smtp-missing-or-code-error/ Share on other sites More sharing options...
Pikachu2000 Posted July 15, 2012 Share Posted July 15, 2012 When posting code, enclose it within the forum's . . . BBCode tags. Quote Link to comment https://forums.phpfreaks.com/topic/265723-some-smtp-missing-or-code-error/#findComment-1361743 Share on other sites More sharing options...
Leko Posted July 15, 2012 Author Share Posted July 15, 2012 Contents of send_form_email.php: <?php if(isset($_POST['email'])) { // set email destination and subject line. $email_to = "[email protected]"; $email_subject = "CWCourier.com Price Quote Request"; function died($error) { // your error code can go here echo "We are very 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(); } // validation that expected data exists if(!isset($_POST['name']) || !isset($_POST['telephone']) || !isset($_POST['email']) || !isset($_POST['from_city']) || !isset($_POST['to_city']) || !isset($_POST['description']) || !isset($_POST['dimensions']) || !isset($_POST['delivery_time'])) { died('There appears to be missing information in the form you submitted. Please go back and supply all required information so that we can supply your quote as accurately as possible.'); } $name = $_POST['name']; // required $telephone = $_POST['telephone']; // required $email_from = $_POST['email']; // required $from_city = $_POST['from_city']; // required $to_city = $_POST['to_city']; // required $description = $_POST['description']; // required $dimensions = $_POST['dimensions']; // required $delivery_time = $_POST['delivery_time']; // required $flexible = $_POST['flexible']; // automatic $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,$name)) { $error_message .= 'The name you entered does not appear to be valid.<br />'; } if(!preg_match($string_exp,$from_city) || !preg_match($string_exp,$to_city)) { $error_message .= 'A city you entered does not appear to be valid.<br />'; } if(strlen($description) < 2) { $error_message .= 'The description you entered does not appear to be valid.<br />'; } if(strlen($dimensions) < 2) { $error_message .= 'The dimensions you entered do not appear to be valid.<br />'; } if(strlen($delivery_time) < 2) { $error_message .= 'The delivery time you entered does not appear to be valid.<br />'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Quote request details:\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "Name: ".clean_string($name)."\n"; $email_message .= "Phone: ".clean_string($telephone)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Pickup City: ".clean_string($from_city)."\n"; $email_message .= "Delivery City: ".clean_string($to_city)."\n"; $email_message .= "Description of Shipment: ".clean_string($description)."\n"; $email_message .= "Dimensions & Weight: ".clean_string($dimensions)."\n"; $email_message .= "Date and Time of Delivery: ".clean_string($delivery_time)."\n"; $email_message .= "Delivery Time is (Flexible or Mandatory): ".clean_string($flexible)."\n"; // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); ?> <!-- success response --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>CW Courier - Quote</title> <meta name="keywords" content="" /> <meta name="description" content="" /> <link href="default.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="header"> <h1> </h1> <h2> </h2> </div> <div id="menu"> <ul> <li><a href="index.html" accesskey="1" title="">Home</a></li> <li><a href="services.html" accesskey="2" title="">Services</a></li> <li><a href="#" accesskey="3" title="">Price Quote</a></li> <li><a href="testimonials.html" accesskey="4" title="">Testimonials</a></li> <li><a href="routes.html" accesskey="5" title="">Routes</a></li> </ul> </div> <div id="content"><div class="inner_copy"></div> <div id="colOne"> <h2>Thank you</h2> Thank you for requesting a price quote with CW Courier. We will look over your requirements immediately and get back to you. Good day! </div> <div style="clear:both;"> </div> </div> <div id="footer"> <div class="fleft"> <p>Copyright 2012, Tharsis Media. All rights reserved.</p></div><div class="fright"></div><div class="fcenter"><p> </p></div> </div> </body> </html> <?php } ?> Contents of HTML page that references PHP script: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>CW Courier - Quote</title> <meta name="keywords" content="" /> <meta name="description" content="" /> <link href="default.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="header"> <h1> </h1> <h2> </h2> </div> <div id="menu"> <ul> <li><a href="index.html" accesskey="1" title="">Home</a></li> <li><a href="services.html" accesskey="2" title="">Services</a></li> <li><a href="#" accesskey="3" title="">Price Quote</a></li> <li><a href="testimonials.html" accesskey="4" title="">Testimonials</a></li> <li><a href="routes.html" accesskey="5" title="">Routes</a></li> </ul> </div> <div id="content"><div class="inner_copy"></div> <div id="colOne"> <h2>receive price quote (all fields required)</h2> <form name="contactform" method="post" action="send_form_email.php"> <table width="450px"> <tr> <td valign="top"> <label for="name">First & Last Name *</label> </td> <td valign="top"> <input type="text" name="name" maxlength="50" size="30"> </td> </tr> <tr> <td valign="top""> <label for="telephone">Telephone Number *</label> </td> <td valign="top"> <input type="text" name="telephone" maxlength="20" size="30"> </td> </tr> <tr> <td valign="top"> <label for="email">Email Address *</label> </td> <td valign="top"> <input type="text" name="email" maxlength="50" size="30"> </td> </tr> <tr> <td valign="top"> <label for="from_city">City and State of Pickup *</label> </td> <td valign="top"> <input type="text" name="from_city" maxlength="40" size="30"> </td> </tr> <tr> <td valign="top"> <label for="to_city">City and State of Delivery *</label> </td> <td valign="top"> <input type="text" name="to_city" maxlength="40" size="30"> </td> </tr> <tr> <td valign="top"> <label for="description">Description of Shipment *</label> </td> <td valign="top"> <textarea name="description" maxlength="200" cols="24" rows="3"></textarea> </td> </tr> <tr> <td valign="top"> <label for="dimensions">Dimensions & Weight *</label> </td> <td valign="top"> <textarea name="dimensions" maxlength="200" cols="24" rows="3"></textarea> </td> </tr> <tr> <td valign="top"> <label for="delivery_time">Date and Time of Delivery *</label> </td> <td valign="top"> <input type="text" name="delivery_time" maxlength="60" size="30"> </td> </tr> <tr> <td valign="top"> <label for="flexible">Delivery Time is * </label> </td> <td valign="top"> <input type="radio" name="flexible" value="Flexible" checked>Flexible <input type="radio" name="flexible" value="Mandatory">Mandatory </td> </tr> <td colspan="2" style="text-align:center"> <input type="submit" value="Submit Quote Request"></a> </td> </tr> </table> </form> </div> <div style="clear:both;"> </div> </div> <div id="footer"> <div class="fleft"> <p>Copyright 2012, Tharsis Media. All rights reserved.</p></div><div class="fright"></div><div class="fcenter"><p> </p></div> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/265723-some-smtp-missing-or-code-error/#findComment-1361744 Share on other sites More sharing options...
Pikachu2000 Posted July 15, 2012 Share Posted July 15, 2012 You're suppressing errors with the @ operator in front of the mail() function, and your success message is displayed unconditionally when the form is submitted. You need to check for TRUE from mail(), and only then show the success message. If FALSE, log the error. Also important to note is that just because mail() returns TRUE, doesn't mean the email will actually be sent and delivered. All it means is that the SMTP server accepted the message. Quote Link to comment https://forums.phpfreaks.com/topic/265723-some-smtp-missing-or-code-error/#findComment-1361745 Share on other sites More sharing options...
Leko Posted July 16, 2012 Author Share Posted July 16, 2012 Ah, ok. Thanks for responding. So I put a conditional on the mail function, and now it's not rendering anything after clicking submit. Here's the new send_form_email.php <?php if(isset($_POST['email'])) { // set email destination and subject line. $email_to = "[email protected]"; $email_subject = "CWCourier.com Price Quote Request"; function died($error) { // your error code can go here echo "We are very 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(); } // validation that expected data exists if(!isset($_POST['name']) || !isset($_POST['telephone']) || !isset($_POST['email']) || !isset($_POST['from_city']) || !isset($_POST['to_city']) || !isset($_POST['description']) || !isset($_POST['dimensions']) || !isset($_POST['delivery_time'])) { died('There appears to be missing information in the form you submitted. Please go back and supply all required information so that we can supply your quote as accurately as possible.'); } $name = $_POST['name']; // required $telephone = $_POST['telephone']; // required $email_from = $_POST['email']; // required $from_city = $_POST['from_city']; // required $to_city = $_POST['to_city']; // required $description = $_POST['description']; // required $dimensions = $_POST['dimensions']; // required $delivery_time = $_POST['delivery_time']; // required $flexible = $_POST['flexible']; // automatic $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,$name)) { $error_message .= 'The name you entered does not appear to be valid.<br />'; } if(!preg_match($string_exp,$from_city) || !preg_match($string_exp,$to_city)) { $error_message .= 'A city you entered does not appear to be valid.<br />'; } if(strlen($description) < 2) { $error_message .= 'The description you entered does not appear to be valid.<br />'; } if(strlen($dimensions) < 2) { $error_message .= 'The dimensions you entered do not appear to be valid.<br />'; } if(strlen($delivery_time) < 2) { $error_message .= 'The delivery time you entered does not appear to be valid.<br />'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Quote request details:\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "Name: ".clean_string($name)."\n"; $email_message .= "Phone: ".clean_string($telephone)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Pickup City: ".clean_string($from_city)."\n"; $email_message .= "Delivery City: ".clean_string($to_city)."\n"; $email_message .= "Description of Shipment: ".clean_string($description)."\n"; $email_message .= "Dimensions & Weight: ".clean_string($dimensions)."\n"; $email_message .= "Date and Time of Delivery: ".clean_string($delivery_time)."\n"; $email_message .= "Delivery Time is (Flexible or Mandatory): ".clean_string($flexible)."\n"; // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); if (mail($email_to, $email_subject, $email_message, $headers)) { ?> <!-- success response --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>CW Courier - Quote</title> <meta name="keywords" content="" /> <meta name="description" content="" /> <link href="default.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="header"> <h1> </h1> <h2> </h2> </div> <div id="menu"> <ul> <li><a href="index.html" accesskey="1" title="">Home</a></li> <li><a href="services.html" accesskey="2" title="">Services</a></li> <li><a href="#" accesskey="3" title="">Price Quote</a></li> <li><a href="testimonials.html" accesskey="4" title="">Testimonials</a></li> <li><a href="routes.html" accesskey="5" title="">Routes</a></li> </ul> </div> <div id="content"><div class="inner_copy"></div> <div id="colOne"> <h2>Thank you</h2> Thank you for requesting a price quote with CW Courier. We will look over your requirements immediately and get back to you. Good day! </div> <div style="clear:both;"> </div> </div> <div id="footer"> <div class="fleft"> <p>Copyright 2012, Tharsis Media. All rights reserved.</p></div><div class="fright"></div><div class="fcenter"><p> </p></div> </div> </body> </html> <?php } else { echo("<p>Message delivery failed, please go back and try again.</p>"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265723-some-smtp-missing-or-code-error/#findComment-1361863 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.