How To Log Emails Sent With PHP's mail() Function To Detect Form Spam

Discussion in 'Server Operation' started by princebenin, Aug 6, 2009.

  1. princebenin

    princebenin New Member

  2. falko

    falko Super Moderator ISPConfig Developer

    Any errors in your mail log?
  3. princebenin

    princebenin New Member

    Thank you Till for the correction.

    The howto is very useful.
  4. sajo

    sajo New Member

    I try to make this script working several times but no luck.
    I have same problem, the script is logging but no emails is sent out.
    I check mail.log and there is fatal error with sendmail.

    Jan 21 20:54:44 server imapdConnectionip=[::ffff:]
    Jan 21 20:54:44 server imapdLOGINuser=sajoip=[::ffff:], port=[55615], protocol=IMAP
    Jan 21 20
    :54:44 server postfix/sendmail[14649]: fatalusagesendmail [options]
    Jan 21 20:54:44 server imapdLOGOUTuser=sajoip=[::ffff:], headers=0body=0rcvd=430sent=327time=0

    this is my phpsendmail file

    server:/home/damjan# vi /etc/php5/apache2/php.ini
    server:/home/damjan# vi /usr/local/bin/phpsendmail

      This script is a sendmail wrapper for php to log calls of the php mail() function.
      Author: Till Brehm,
      (Hopefully) secured by David Goodwin <david @>

    $sendmail_bin '/usr/sbin/sendmail';
    $logfile '/var/log/mail.form';

    //* Get the email content
    $logline '';
    $pointer fopen('php://stdin''r');

    while (
    $line fgets($pointer)) {
    preg_match('/^to:/i'$line) || preg_match('/^from:/i'$line)) {
    $logline .= trim($line)." \n";
    $mail .= $line;

    //* compose the sendmail command
    $command 'echo ' escapeshellarg($mail) . ' | '.$sendmail_bin.' -t -i';
    for (
    $i 1$i $_SERVER['argc']; $i++) {
    $command .= escapeshellarg($_SERVER['argv'][$i]).' ';

    //* Write the log
    file_put_contents($logfiledate('Y-m-d H:i:s') . ' ' $_ENV['PWD'] . ' ' $loglineFILE_APPEND);
    //* Execute the command
    return shell_exec($command);

    Please, need some help.
    Thanks in advance
  5. falko

    falko Super Moderator ISPConfig Developer

    Does /usr/sbin/sendmail exist? Is the path correct?
  6. sajo

    sajo New Member

    Hello, Falko thanks for help.

    Yes path is correct and file is there.
  7. unai

    unai New Member

    Issues sending UTF-8 encoded emails

    Dear All,

    We have had issues sending UTF-8 encoded emails when using this phpsendmail wrapper instead of the normal sendmail method. Basically the contents get all turned into unreadable characters.

    Has anyone experienced similar issues? Would there be a way of fixing this?

    Thank you so much for the excellent information.

    With Best Wishes,
    Unai Rodriguez

Share This Page