I inherited a website to run that has a full employment section, we'd often get calls from people who swear to the dickens that they submitted an application and we just didnt get it. (HR only used the copy that was emailed to them, most of these Apps were sent to the database)
Through all my investigation i just have no idea why it randomly sometimes doesnt work. I have recently added if(mail()) to submit to a separate database to indicate that it did indeed send the mail.
In the last two days 18 applications have been submitted, all 18 were put into the applications database, but only 16 emails sent, and only 16 if(mail())'s triggered. This is telling me its not on the server SMTP, but mail() just isnt triggering when the form is submitted. What can be the cause of this? Here is the mail() code:
$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
";
$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\"
";
$message .= "Content-Transfer-Encoding: 7bit
";
$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."\"
";
$message .= "Content-Transfer-Encoding: base64
";
$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'));
}
In the last two days 18 applications have been submitted, all 18 were put into the applications database, but only 16 emails sent, and only 16 if(mail())'s triggered. This is telling me its not on the server SMTP, but mail() just isnt triggering when the form is submitted.
What's happening is that the mail
function is returning FALSE
, which is why you are not seeing any entries in the database.
It will return FALSE
if the SMTP server refused the email for delivery; so check the logs of the email server you are using.
Even if it returns TRUE
(meaning, the if
is triggered) there is no guarantee the mail will be delivered - it just means the email was accepted by the SMTP server for transmission.