<?php
$to = "smtp@local.com";
$from = "php@local.com";
$subject = "Local email";
$message = "This email has been sent locally.";
$mailToName = "SMTP";
$mailFromName = "PHP";
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=utf-8";
$headers[] = "To: {$mailToName} <{$to}>";
$headers[] = "From: {$mailFromName} <{$from}>";
$headers = implode("\r\n", $headers);
$isAccepted = mail($to, $subject, $message, $headers);
$today = date("F j, Y, H:i:s");
$message = ($isAccepted ? "mail accepted for delivery" : "mail not accepted");
$message .= " | " . $today . "\r\n";
$messageLines = 5e5; // five hundred thousand lines
$m = "";
for ($x=0; $x < $messageLines; $x++) {
$m .= $message;
}
$message = $m;
function prepend($string, $filename){
if (file_exists($filename)) {
$context = stream_context_create ();
$fp = fopen($filename,'r',1,$context);
$tmpname = md5($string);
file_put_contents($tmpname,$string);
file_put_contents($tmpname,$fp,FILE_APPEND);
fclose($fp);
unlink($filename);
rename($tmpname,$filename);
} else {
if (!file_exists(dirname($filename))) {
mkdir(dirname($filename), 0700, TRUE);
}
file_put_contents($filename,$string);
}
}
function count_lines($filename) {
if (file_exists($filename)) {
if ($fp = fopen($filename,'r')) {
$totalLines = 0;
while (($buffer = fgets($fp, 4096)) !== FALSE) {
++$totalLines;
}
if (!feof($fp)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($fp);
return $totalLines;
}
}
return NULL;
}
function get_logfilename($char) {
return "logs".DIRECTORY_SEPARATOR."mail{$char}.log";
}
// count existing log files
$fileCount = 0;
$files = glob(get_logfilename("*"));
if ($files !== FALSE) {
$fileCount = !empty($files) ? count($files) : 0;
}
// store log messages in logfile(s)
if ($fileCount < 1) {
$logFile = get_logfilename(1);
prepend($message, $logFile);
} else {
$maxLines = 1e6; // one million lines
$canCreateNewLogFile = (int) count_lines(get_logfilename($fileCount)) >= $maxLines;
$logFile = get_logfilename($canCreateNewLogFile ? ++$fileCount : $fileCount);
prepend($message, $logFile);
}
?>
For people who feel the need to log some information during an attempt to send an email. Tested on localhost with Test Mail Server Tool.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.