为什么PHP的邮件功能无法间歇性地发送?

时间:2017-03-29 01:06:10

标签: php email

每当用户成功在我的网站上提交表单时,我都会触发一个事件。我可以跟踪从Google Analytics帐户中触发事件的确切时间。事件触发的时间反映了发送到我收件箱的电子邮件的发送时间(雅虎邮件)。

但是,我多次注意到,当同一事件发生时,没有电子邮件发送到同一个电子邮件地址。它永远不会到达目的地。

我该如何解决这个问题?这个问题的根源是什么?

这是我的php代码:

<?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.';
}
 }
  ?>

1 个答案:

答案 0 :(得分:0)

当邮件传递到SMTP服务器时,

mail()的传递功能结束。它的唯一责任就是把你的信封放到角落里的邮箱里。邮政服务的其余部分(清空该邮箱,通过处理中心运行,将其飞到收件人的国家/城市等等)完全不在mail()的范围之内。只要信封放入邮箱,mail()就会返回true并假装它已被传送。

所以......检查你的SMTP服务器的日志,看看邮件到底发生了什么。也许它被接收器标记为垃圾邮件并被弹回。也许它被困在某个地方的队列等等......只有日志会告诉你 - 你在PHP中可以看到/做的任何东西都是无用的,因为PHP和mail()只占电子邮件发送/交付过程的1%,其他99%的东西出了问题。