0

I have an event that fires every time a user successfully submits a form on my website. I can track the exact time that the event fired from my Google Analytics account. The time that the event fired, mirrors the delivery time of the email delivered to my inbox (yahoo mail).

However, I've noticed many times that, when the same event fires, no email is delivered to the the same email address. It never reaches its destination.

How can I fix this? What is the root of this problem?

Here is my php code:

<?php
  if($_POST)
 {
   //check if its an ajax request, exit if not
   if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND      strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
    die();
} 


//check $_POST vars are set, exit if any missing
if(!isset($_POST["userName"]) || !isset($_POST["userLastName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userArrival"]) || !isset($_POST["userNumberPeople"]) 
|| !isset($_POST["userPickupLocation"]) || !isset($_POST["userRequest"]) || !isset($_POST["userTourSelection"]))
{
    die();
}

//Sanitize input data using PHP filter_var().
$user_Name              = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
$user_LastName          = filter_var($_POST["userLastName"], FILTER_SANITIZE_STRING);
$user_Email             = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
$user_TourSelection     = filter_var($_POST["userTourSelection"], FILTER_SANITIZE_STRING);
$user_Arrival           = filter_var($_POST["userArrival"], FILTER_SANITIZE_STRING);
$user_NumberPeople      = filter_var($_POST["userNumberPeople"], FILTER_SANITIZE_STRING);
$user_PickupLocation    = filter_var($_POST["userPickupLocation"], FILTER_SANITIZE_STRING);
$user_Request           = filter_var($_POST["userRequest"], FILTER_SANITIZE_STRING);



 $to_Email       = "something@yahoo.com"; //Replace with recipient email address
 $subject        = 'Tour request: '.$user_TourSelection.' '; //Subject line for emails


//additional php validation
if(strlen($user_Name)<2) // If length is less than 4 it will throw an HTTP error.
{
    header('HTTP/1.1 500 Name is too short or empty!');
    exit();
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
{
    header('HTTP/1.1 500 Please enter a valid email address!');
    exit();
}
if(strlen($user_Request)<5) //check emtpy message
{
    header('HTTP/1.1 500 Please explain in a few words how you would like us to assist you.');
    exit();
}


// message
 $message = '<strong>Name:</strong> '.$user_Name.' '.$user_LastName.' <br><br>
        <strong>Date of Arrival:</strong> '.$user_Arrival.' <br><br>
        <strong>Tour:</strong> '.$user_TourSelection.' <br><br>
        <strong>Number of people:</strong> '.$user_NumberPeople.' <br><br>
        <strong>Pick-Up Location:</strong> '.$user_PickupLocation.' <br><br>
        <strong>Request:</strong> '.$user_Request.' 
        ';


//proceed with PHP email.
$headers = 'From: '.$user_Email.'' . "\r\n" .
$headers .='Reply-To: '.$user_Email.'' . "\r\n" ;
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .='X-Mailer: PHP/' . phpversion();

 @$sentMail = mail($to_Email, $subject, $message, $headers);

if(!$sentMail)
{
    header('HTTP/1.1 500 Our server is under maintenance!<br> If this error persists please contact us directly: <strong>something@yahoo.com</strong>');
    exit();
}else{
    echo 'Congratulations '.$user_Name .'! ';
    echo 'We have received your request and we will respond as soon as possible.';
}
 }
  ?>
Obi-Wan
  • 101
  • 1
  • 8

1 Answers1

0

mail()'s delivery function ends when it hands off your mail to the SMTP server. Its sole responsibility is the real-world equivalent of taking your envelope and dropping it into the mailbox on the corner. The rest of the postal service (emptying that box, running it through processing centers, flying it to the recipient's country/city, etc...) is completely outside of mail()'s scope. As long as the envelope drops into the mailbox, mail() will return true and pretend it was delivered.

So... check your SMTP server's logs to see what really happened to the mail. Maybe it got marked as spam by the receiver and bounced. Maybe it's stuck in a queue somewhere, etc... Only the logs will tell you this - anything you can see/do in PHP is useless, because PHP and mail() only do maybe 1% of the email sending/delivery process, and something's wrong in that other 99%.

Otávio Barreto
  • 1,536
  • 3
  • 16
  • 35