mail() 有时不会在表单提交时触发


mail() sometimes not triggered on form submit

我继承了一个网站,该网站有一个完整的就业部分,我们经常接到人们的电话,他们向狄更斯发誓他们提交了申请,我们只是没有收到。(HR只使用通过电子邮件发送给他们的副本,这些应用程序中的大多数都发送到数据库)

通过我所有的调查,我只是不知道为什么它有时随机不起作用。 我最近添加了 if(mail()) 以提交到一个单独的数据库,以表明它确实发送了邮件。

在过去的两天里,已经提交了18份申请,

所有18份都被放入了申请数据库,但只发送了16封电子邮件,只有16封if(mail())被触发。这告诉我它不在服务器 SMTP 上,但 mail() 只是在提交表单时没有触发。这是什么原因造成的? 这是 mail() 代码:

$mailto = "$setting[apps_email]";
$from_name = 'Employment Application';
$from_mail = 'no-reply@....com';
$replyto = 'no-reply@....com';
$uid = md5(uniqid(time())); 
$subject = "".$row[fname]." ".$row[lname]." - ".$row1[position]."";
$filename = "".$row[fname]."".$row[lname]."-".$row[submitted].".pdf";

$header = "From: ".$from_name." <".$from_mail.">".$eol;
$header .= "Reply-To: ".$replyto.$eol;
$header .= "MIME-Version: 1.0'r'n";
$header .= "Content-Type: multipart/mixed; boundary='"".$uid."'"".$eol;
$header .= "Content-Transfer-Encoding: 7bit".$eol;
$message .= "--".$uid.$eol;
$message .= "Content-type:text/plain; charset='"iso-8859-1'"'r'n";
$message .= "Content-Transfer-Encoding: 7bit'r'n'r'n";
$message .= $eol."".$row[fname]." ".$row[lname]." has submitted an employment application for the ".$row1[position]." position. Please see the attached .pdf file to save and/or print the application.".$eol;
$message .= "--".$uid.$eol;
$message .= "Content-Type: application/pdf; name='"".$filename."'"'r'n";
$message .= "Content-Transfer-Encoding: base64'r'n";
$message .= "Content-Disposition: attachment".$eol.$eol;
$message .= $eol.$content;
$message .= "--".$uid."--".$eol;
$fail_date = date('Y-m-d');
if(mail($mailto, $subject, $message, $header)){
    $sql = "INSERT INTO failed_apps (position, name, date, num) values (?, ?, ?, ?)");
    $q = $db->prepare($sql);
    $q->execute(array('$row1[position]', '$_POST[fname] $_POST[lname]', '$fail_date', 'Emailed Succesfully'));
}   

在过去的两天里,已经提交了18份申请,所有18份申请都是 放入应用程序数据库,但只发送了 16 封电子邮件,而且只有 16 if(mail()) 被触发。这告诉我它不在服务器上 SMTP,但 mail() 只是在提交表单时不会触发。

发生的事情是 mail 函数返回 FALSE ,这就是为什么您在数据库中看不到任何条目的原因。

如果SMTP服务器拒绝发送电子邮件,它将返回FALSE;因此请检查您正在使用的电子邮件服务器的日志。

即使它返回TRUE(意味着触发if),也不能保证邮件会被传递 - 它只是意味着电子邮件已被SMTP服务器接受进行传输。