Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add a timestamp to the mail log. #246

Closed
wants to merge 2 commits into from

3 participants

@mj
mj commented

This patch is loosely based on the one in bug #52126 but instead of using a UNIX timestamp it uses the date format also being used by error_log et al.

@mj mj Add a timestamp to the mail log.
This patch is loosely based on the one in bug #52126 but instead of
using a UNIX timestamp it uses the date format also being used by
error_log et al.
94a89bf
@lstrojny

It would be good to have a test for that. Not sure if it is possible though.

@lstrojny

ping @mj

@mj
mj commented

Lars, I have added a test for mail.log now.

@php-pulls
Collaborator

Comment on behalf of lstrojny at php.net:

Merged into 5.5 and master.

@php-pulls php-pulls closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 25, 2012
  1. @mj

    Add a timestamp to the mail log.

    mj authored
    This patch is loosely based on the one in bug #52126 but instead of
    using a UNIX timestamp it uses the date format also being used by
    error_log et al.
Commits on Jan 6, 2013
  1. @mj
This page is out of date. Refresh to see the latest.
Showing with 59 additions and 2 deletions.
  1. +11 −2 ext/standard/mail.c
  2. +48 −0 ext/standard/tests/mail/mail_log.phpt
View
13 ext/standard/mail.c
@@ -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>
@@ -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);
View
48 ext/standard/tests/mail/mail_log.phpt
@@ -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
Something went wrong with that request. Please try again.