Skip to content

Add a timestamp to the mail log. #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions ext/standard/mail.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <time.h>
#include "php.h"
#include "ext/standard/info.h"
#include "ext/standard/php_string.h"
#include "ext/standard/basic_functions.h"
#include "ext/date/php_date.h"

#if HAVE_SYSEXITS_H
#include <sysexits.h>
Expand Down Expand Up @@ -246,8 +248,15 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char
return val; \

if (mail_log && *mail_log) {
char *tmp;
int l = spprintf(&tmp, 0, "mail() on [%s:%d]: To: %s -- Headers: %s\n", zend_get_executed_filename(TSRMLS_C), zend_get_executed_lineno(TSRMLS_C), to, hdr ? hdr : "");
char *tmp, *date_str;
time_t curtime;

time(&curtime);
date_str = php_format_date("d-M-Y H:i:s e", 13, curtime, 1 TSRMLS_CC);

int l = spprintf(&tmp, 0, "[%s] mail() on [%s:%d]: To: %s -- Headers: %s\n", date_str, zend_get_executed_filename(TSRMLS_C), zend_get_executed_lineno(TSRMLS_C), to, hdr ? hdr : "");

efree(date_str);

if (hdr) {
php_mail_log_crlf_to_spaces(tmp);
Expand Down
48 changes: 48 additions & 0 deletions ext/standard/tests/mail/mail_log.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--TEST--
Test mail() function : mail.log ini setting
--INI--
sendmail_path=tee /tmp/mail.out >/dev/null
mail.log = /tmp/mail.log
--SKIPIF--
<?php
if(substr(PHP_OS, 0, 3) == "WIN")
die("skip Won't run on Windows");
?>
--FILE--
<?php
date_default_timezone_set("UTC");

$logfile = ini_get("mail.log");
if (file_exists($logfile)) {
unlink($logfile);
}
touch($logfile);
clearstatcache();

$to = "test@example.com";
$subject = "mail.log test";
$message = "Testing mail.log";
$headers = "X-Test: 1";

var_dump(filesize($logfile) == 0);
clearstatcache();

var_dump(mail($to, $subject, $message, $headers));

var_dump(filesize($logfile) > 0);
clearstatcache();

echo file_get_contents($logfile);
?>
Done
--CLEAN--
<?php
unlink("/tmp/mail.log");
unlink("/tmp/mail.out");
?>
--EXPECTF--
bool(true)
bool(true)
bool(true)
[%d-%s-%d %d:%d:%d UTC] mail() on [%smail_log.php:%d]: To: test@example.com -- Headers: X-Test: 1
Done