Jump to content
memorabeerlia

PHP Form-> Email not sending

Recommended Posts

Hello,

 

I created an order form on my website that takes the user's inputs, send confirmation emails to them and myself (order@), and then inserts the order into a mySQL database. The emails used to work, but now, for some reason, they never get sent. The email address it is supposed to be sending it to works and I have validated that nothing is getting blocked in SPAM, etc. The code for the email portion is below. I have tried debugging myself, but I get no errors on the mail() portion of the code. Any help that could be offered would be greatly appreciated. Thank you.

if(isset($_POST['email'])) {     // EDIT THE 2 LINES BELOW AS REQUIRED     $email_to = "order@idrinktoremember.com";     $email_subject = "We've Got an Order!";         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 expected data exists     if(!isset($_POST['first_name']) ||         !isset($_POST['last_name']) ||         !isset($_POST['email']) 	||         !isset($_POST['telephone']) ||		!isset($_POST['address']) 	||		!isset($_POST['city'])		||		!isset($_POST['state'])		||		!isset($_POST['zip'])		||		!isset($_POST['opener'])	||		!isset($_POST['stain'])		||		!isset($_POST['memory'])	||		!isset($_POST['catcher']))	{        died('We are sorry, but please ensure you filled in all required items.  Click back to check.');    }     $first_name = $_POST['first_name'];		// required     $last_name = $_POST['last_name'];		// required     $email_from = $_POST['email']; 			// required     $telephone = $_POST['telephone']; 		// required	$address = $_POST['address'];			// required	$city = $_POST['city'];					// required	$state = $_POST['state'];				// required	$zip = $_POST['zip'];					// required	$opener = $_POST['opener'];				// required	$stain = $_POST['stain'];				// required	$memory = $_POST['memory'];				// required	$catcher = $_POST['catcher'];			// required    $comments = $_POST['comments']; 		// not 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($error_message) > 0) {     died($error_message);   }     $email_message = "Order details below.nn";         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 .= "Address: ".clean_string($address)."n";$email_message .= "City: ".clean_string($city)."n";$email_message .= "State: ".clean_string($state)."n";$email_message .= "Zip Code: ".clean_string($zip)."n";$email_message .= "Opener: ".clean_string($opener)."n";$email_message .= "Stain Selected: ".clean_string($stain)."n";$email_message .= "Memory Requested: ".clean_string($memory)."n";$email_message .= "Catcher Requested: ".clean_string($catcher)."n";$email_message .= "Comments: ".clean_string($comments)."n"; //create email headers $email_us = "order@idrinktoremember.com";$headers  = "From: $email_us rn";$headers .= "Reply-To: $email_us rn";$headers .= "Return-Path: $email_usrn";$headers .= "X-Mailer: PHP rn";if(mail($email_to, $email_subject, $email_message, $headers)){	echo '';}else{	echo 'Error';}//send order confirmation email$email_subject_confirmation = "Congratulations, your order has been submitted!";$email_message_confirmation .= "The details of your order are below. Please review.nn";$email_message_confirmation .= "First Name: ".clean_string($first_name)."n"; $email_message_confirmation .= "Last Name: ".clean_string($last_name)."n"; $email_message_confirmation .= "Email: ".clean_string($email_from)."n"; $email_message_confirmation .= "Telephone: ".clean_string($telephone)."n";$email_message_confirmation .= "Address: ".clean_string($address)."n";$email_message_confirmation .= "City: ".clean_string($city)."n";$email_message_confirmation .= "State: ".clean_string($state)."n";$email_message_confirmation .= "Zip Code: ".clean_string($zip)."n";$email_message_confirmation .= "Opener Selected: ".clean_string($opener)."n";$email_message_confirmation .= "Stain Selected: ".clean_string($stain)."n";$email_message_confirmation .= "Memory Requested: ".clean_string($memory)."n";$email_message_confirmation .= "Catcher Requested: ".clean_string($catcher)."n";$email_message_confirmation .= "Comments: ".clean_string($comments)."n";$email_message_confirmation .= "nnWe will be contacting you shortly with a prototype of your design!n";$email_message_confirmation .= "nThe MemoraBEERLia Team";$headers  = "From: $email_us rn";$headers .= "Reply-To: $email_us rn";$headers .= "Return-Path: $email_usrn";$headers .= "X-Mailer: PHP rn";if(mail($email_from, $email_subject_confirmation, $email_message_confirmation, $headers)){	echo '';}else{	echo 'Error';}

Share this post


Link to post
Share on other sites

Is error reporting on?

Share this post


Link to post
Share on other sites

No errors = Time to start taking it apart and testing it incrementally.

Share this post


Link to post
Share on other sites

I've been looking over your code, but at the same time noting this is only part of the whole, and don't see anything out of order, at least nothing that jumps out at me. That being said let's look at your description of the problem you "began having at some point in time," but "before that point in time" it was working fine, right? Now unless you made changes to the code in question at the time it started malfunctioning, then do you know when the problem started, and what might have changed? Was there a software upgrade(s), a program ini change, or new code - software added that might affect your application? Have you checked your send mail settings in the php ini file?

 

What I am saying here is that maybe it's not your code because it doesn't seem to be creating any kind of error, and it seems to me, from looking over your code, you have error traps set at every point so it should flag something, and so it would seem the problem might be server - host related, or a setting has been changed due to an upgrade. I manage 27 domains over 3 different host for clients, and I am always having headaches from the unintended consequences of upgrades.

 

Like I said, I'm only seeing part of your whole app, or order form, and I would surmise it's all part of a shopping cart, but I don't understand why you’re going to all the trouble of validating the email instead of leaving validation to the form then, sending the email on a successful submit to the database, but then again I could be misreading your post too. Now I do something that is very similar with a equipment pickup request form, which just like your sales form, is a request for services to be rendered, right, but I do it all with the form, then on submit with a successful insert, a script query’s the database for the information (already clean and valid) to send in the confirmation email.

Edited by Count_Zero

Share this post


Link to post
Share on other sites

OK. I have changed it up and now I am getting a different issue. Instead of the above code I am using phpMailer. For some reason, I am getting this error:

 

2014-12-12 03:46:21
2014-12-12 03:46:21 Connection: opened 2014-12-12 03:46:26 SERVER -> CLIENT:
2014-12-12 03:46:26 SMTP NOTICE: EOF caught while checking if connected
2014-12-12 03:46:26 Connection: closed
2014-12-12 03:46:26 SMTP connect() failed. Message could not be sent.Mailer Error: SMTP connect() failed
Below is the code. I am getting an email to the email address the USER input, but I am not getting an email address to the static email address of "order@idrinktoremember.com." I appreciate the help. Thanks!
<?php session_start();require 'phpmailer/PHPMailerAutoload.php';$mail = new PHPMailer;$mail->SMTPDebug = 3;                               // Enable verbose debug output$mail->isSMTP();                                      // Set mailer to use SMTP$mail->Host = 'smtp.idrinktoremember.com'; 			  // Specify main server$mail->SMTPAuth = true;                               // Enable SMTP authentication$mail->Username = '';                 // SMTP username$mail->Password = '';                           // SMTP password$mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted$mail->Port = 465;                                    // TCP port to connect toif(isset($_POST['email'])) {     // EDIT THE 2 LINES BELOW AS REQUIRED     $email_subject = "We've Got an Order!";         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 "Please go back and fix these errors.<br /><br />";         die();     }      // validation expected data exists     if(!isset($_POST['first_name']) ||         !isset($_POST['last_name']) ||         !isset($_POST['email']) 	||         !isset($_POST['telephone']) ||		!isset($_POST['address']) 	||		!isset($_POST['city'])		||		!isset($_POST['state'])		||		!isset($_POST['zip'])		||		!isset($_POST['opener'])	||		!isset($_POST['stain'])		||		!isset($_POST['memory'])	||		!isset($_POST['catcher']))	{        died('We are sorry, but please ensure you filled in all required items.  Click back to check.');    }	include_once $_SERVER['DOCUMENT_ROOT'] . '/securimage/securimage.php';	$securimage = new Securimage();		if ($securimage->check($_POST['captcha_code']) == false) {  	// the code was incorrect	// you should handle the error so that the form processor doesn't continue	// or you can use the following code if there is no validation or you do not know how	echo "The security code entered was incorrect.<br /><br />";	echo "Please go <a href='javascript:history.go(-1)'>back</a> and try again.";	exit;	}     $first_name = $_POST['first_name'];		// required     $last_name = $_POST['last_name'];		// required     $email_from = $_POST['email']; 			// required     $telephone = $_POST['telephone']; 		// required	$address = $_POST['address'];			// required	$city = $_POST['city'];					// required	$state = $_POST['state'];				// required	$zip = $_POST['zip'];					// required	$opener = $_POST['opener'];				// required	$stain = $_POST['stain'];				// required	$memory = $_POST['memory'];				// required	$catcher = $_POST['catcher'];			// required    $comments = $_POST['comments']; 		// not 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($error_message) > 0) {     died($error_message);   }     $email_message = "Order details below.<br><br>";         function clean_string($string) {       $bad = array("content-type","bcc:","to:","cc:","href");       return str_replace($bad,"",$string);     }  $email_message .= "<b>First Name:</b> ".clean_string($first_name)."<br>"; $email_message .= "<b>Last Name:</b> ".clean_string($last_name)."<br>"; $email_message .= "<b>Email: </b>".clean_string($email_from)."<br>"; $email_message .= "<b>Telephone: </b>".clean_string($telephone)."<br>";$email_message .= "<b>Address: </b>".clean_string($address)."<br>";$email_message .= "<b>City:</b> ".clean_string($city)."<br>";$email_message .= "<b>State:</b> ".clean_string($state)."<br>";$email_message .= "<b>Zip Code:</b> ".clean_string($zip)."<br>";$email_message .= "<b>Opener:</b> ".clean_string($opener)."<br>";$email_message .= "<b>Stain Selected:</b> ".clean_string($stain)."<br>";$email_message .= "<b>Memory Requested:</b>".clean_string($memory)."<br>";$email_message .= "<b>Catcher Requested:</b> ".clean_string($catcher)."<br>";$email_message .= "<b>Comments:</b> ".clean_string($comments)."<br>";$email_message .= "<p> - The MemoraBEERlia Team. <br>Drink to Remember!</p>"; //create email headers$mail->From = 'order@idrinktoremember.com';$mail->FromName = 'MemoraBEERlia';$mail->addAddress('order@idrinktoremember.com', 'Order Team');     // Add a recipient$mail->addReplyTo('order@idrinktoremember.com', 'Order Team');$mail->isHTML(true);                                  // Set email format to HTML$mail->Subject = $email_subject;$mail->Body    = $email_message;if(!$mail->send()) {    echo 'Message could not be sent.';    echo 'Mailer Error: ' . $mail->ErrorInfo;} else {    echo '';}//send order confirmation email$email_subject_confirmation = "Congratulations, your order has been submitted!";$email_message_confirmation .= "The details of your order are below. Please review.<br><br>";$email_message_confirmation .= "<b>First Name: </b>".clean_string($first_name)."<br>"; $email_message_confirmation .= "<b>Last Name: </b>".clean_string($last_name)."<br>"; $email_message_confirmation .= "<b>Email: </b>".clean_string($email_from)."<br>"; $email_message_confirmation .= "<b>Telephone: </b>".clean_string($telephone)."<br>";$email_message_confirmation .= "<b>Address: </b>".clean_string($address)."<br>";$email_message_confirmation .= "<b>City: </b>".clean_string($city)."<br>";$email_message_confirmation .= "<b>State: </b>".clean_string($state)."<br>";$email_message_confirmation .= "<b>Zip Code: </b>".clean_string($zip)."<br>";$email_message_confirmation .= "<b>Opener Selected: </b>".clean_string($opener)."<br>";$email_message_confirmation .= "<b>Stain Selected: </b>".clean_string($stain)."<br>";$email_message_confirmation .= "<b>Memory Requested: </b>".clean_string($memory)."<br>";$email_message_confirmation .= "<b>Catcher Requested: </b>".clean_string($catcher)."<br>";$email_message_confirmation .= "<b>Comments: </b>".clean_string($comments)."<br>";$email_message_confirmation .= "<br><br>We will be contacting you shortly with a prototype of your design!  Thank you!<br>";$email_message_confirmation .= "<br>- The MemoraBEERLia Team";$mail = new PHPMailer;$mail->From = 'order@idrinktoremember.com';$mail->FromName = 'MemoraBEERlia';$mail->addAddress($email_from);     // Add a recipient$mail->addReplyTo('order@idrinktoremember.com', 'MemoraBEERlia Team');$mail->isHTML(true);                                  // Set email format to HTML$mail->Subject = $email_subject_confirmation;$mail->Body    = $email_message_confirmation;if(!$mail->send()) {    echo 'Message could not be sent.';    echo 'Mailer Error: ' . $mail->ErrorInfo;} else {    echo '';}
Edited by memorabeerlia

Share this post


Link to post
Share on other sites

So the first email isn't received, and the second one is? Have you checked your spam folders and things like that? Do you see the error text that shows up if the mail failed to send?

Share this post


Link to post
Share on other sites

The configurations for the 2 mail objects are different. This is how you configure the first one:

$mail = new PHPMailer;$mail->SMTPDebug = 3;                               // Enable verbose debug output$mail->isSMTP();                                      // Set mailer to use SMTP$mail->Host = 'smtp.idrinktoremember.com';        // Specify main server$mail->SMTPAuth = true;                               // Enable SMTP authentication$mail->Username = '';                 // SMTP username$mail->Password = '';                           // SMTP password$mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted$mail->Port = 465;    $mail->From = 'order@idrinktoremember.com';$mail->FromName = 'MemoraBEERlia';$mail->addAddress('order@idrinktoremember.com', 'Order Team');     // Add a recipient$mail->addReplyTo('order@idrinktoremember.com', 'Order Team');$mail->isHTML(true);                                  // Set email format to HTML$mail->Subject = $email_subject;$mail->Body    = $email_message; 
and the second one:
$mail = new PHPMailer;$mail->From = 'order@idrinktoremember.com';$mail->FromName = 'MemoraBEERlia';$mail->addAddress($email_from);     // Add a recipient$mail->addReplyTo('order@idrinktoremember.com', 'MemoraBEERlia Team');$mail->isHTML(true);                                  // Set email format to HTML$mail->Subject = $email_subject_confirmation;$mail->Body    = $email_message_confirmation;
You can experiment with the various settings to figure out which one(s) cause the failure.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...