Инструменты пользователя

Инструменты сайта


programming:php:sendmail_logger_php_wrapper

Sendmail логирование на php

/usr/local/sbin/sendmail_logged

#!/usr/bin/php
<?php
/* Sendmail Wrapper 1.0
By Greg Maclellan (www.gregmaclellan.com)

This is a wrapper for sendmail. All you have to do is set the paths below,
and set up this script to be called.

For php, it means changing sendmail_path to point at this file. 
*/

// unique message id to use for this message
$messageid = uniqid("logged");

// log file to write to (should use $messageid)
$logfile = "/var/log/phpmail/".$messageid;

// path to sendmail 
$sendmail = "/usr/sbin/sendmail -t -i";

// additional headers
$add_headers["X-MsgID"] = $messageid;

// parse out the domain from the current working directory. In this case, named /home/httpd/vhosts/domain.com/. 
if (preg_match("|/home/httpd/vhosts/([a-zA-Z0-9\._-]*)(/.*)?|", $_ENV["PWD"], $matches)) {
    $add_headers["X-Generating-Domain"] = $matches[1];
}

/*****************************************************************************
 *****************************************************************************/

// read STDIN (mail message)
while (!feof(STDIN)) {
    $data .= fread(STDIN, 1024);
}

// split out headers
list ($headers, $message) = explode("\n\n", $data, 2); 

// add additional headers
if ($add_headers) {
    foreach ($add_headers as $field=>$contents) {
        $headers .= "\n".$field.": ".$contents;
    }
}

// reassemble the message
$data = $headers."\n\n".$message;


// write to our log file
$fd = fopen($logfile,"w");
fwrite($fd, "Date: ".date("r")."\n");
fwrite($fd, "PWD: ".$_ENV['PWD']."\n");
fwrite($fd, "Msg ID: ".$messageid."\n");
fwrite($fd, "\n");

fwrite($fd, "Message:\n");
fwrite($fd, "----------\n");
fwrite($fd, $headers);
fwrite($fd, "----------\n");
fwrite($fd, "Body: ".strlen($message)." bytes");

fclose($fd);

// write to the real sendmail
$h = popen($sendmail, "w");
fwrite($h, $data);
pclose($h);


?>
#!/bin/bash

# Greg MacLellan 12/16/2005

# Script for reporting how much mail domains are sending out
# http://gregmaclellan.com/php/
# Release notes: http://gregmaclellan.com/blog/sendmail-wrapper/

# Put this script in cron as, eg:
#     0 0 * * * /usr/local/sbin/mailreporting.sh delete 
# for daily reports, or
#     0 0 * * 0 /usr/local/sbin/mailreporting.sh delete 
# for weekly reports.
# You can also run it directly (without specifying delete) to get a count of the mail per-domain


# path to the mail directory
MAILPATH=/var/mail_log

# file to use as "compare" file
# When running in delete mode, the script will delete any
# mail log files older than this file, then set the date
# on this file to the current date. Default is to use $0 
# (this file)
TESTFILE=$0

# header to check for (grep)
HEADER="X-Generating-Domain"

if [ "$1" == "delete" ]; then
    find $MAILPATH -not -cnewer $TESTFILE | xargs rm
    touch $TESTFILE
    
    echo "Mail sent by doamins between "`date -r $TESTFILE  +%H:%M:%S\ %M\/%d\/%Y`" and "`date +%H:%M:%S\ %M\/%d\/%Y`
fi

    
grep "$HEADER" $MAILPATH/* | cut -f 2 -d\  | sort | uniq -c -d
programming/php/sendmail_logger_php_wrapper.txt · Последнее изменение: 2017/03/27 11:26 — artur

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki