To implement the logging of the success (and failed) mail() calls, make the following changes to the code.
Add the following lines, near the top of the code -
PHP Code:
// append data to a log file
function _log($log_file,$ip,$email,$message)
{
$str = date('Y-m-d H:i:s') . " IP:$ip, EM:$email, $message\n";
file_put_contents($log_file,$str,FILE_APPEND);
}
$log_file = 'log.txt'; // log text file name
$ip = $_SERVER['REMOTE_ADDR']; // get ip to short variable name for logging purposes
Replace the existing if(mail(...)) ... else ... code with the following -
PHP Code:
if(mail($to, $subject, $message, $headers))
{
echo "Dankje om je gegevens door te zenden.<br>We nemen die door en contacteren je zo snel mogelijk.";
// 6) Even if the mail() call succeeds, there is no guarantee that the email will get sent by the sending mail server or accepted by the receiving mail server. You should log the data/time, email address, ip address, a success message, the total number of uploaded files and either the size of each file or the total size of all the files, and any other values you think may be helpful. This will tell you that there was a form submission and hopefully give information that will help find out why you are not receiving some of the emails.
// log the success information to compare against actual received emails
_log($log_file,$ip,$email,'Mail call successful.');
} else {
echo "Error in mail.<br>Probeer opnieuw door hieronder te klikken.";
// 5) If the mail() call fails, again, you need to log the date/time, email address, ip address, and your own mail fail message, so that you can try to find and fix any problem.
// log the fail information. you can capature the php error message and include it too.
$last_error = error_get_last(); // "type", "message", "file" and "line".
_log($log_file,$ip,$email,"Mail call failed - {$last_error['message']}");
}
Bookmarks