How to get postfix email to log to a file

This is how to get postfix mail transfer agent to log emails to a file instead of sending to the outside SMTP world.

  1. Create a user that will own the e-mail logs (or use an existing one). I called mine "logmails".
  2. useradd -c "Email Log" -d /home/logmails logmails
  3. mkdir /home/logmails
  4. Place the following script in /home/logmails/mail_eater.php (this uses PHP, but you can write in any language you like, it just appends stdin to a file):
  5. #!/usr/bin/php
    <?php
    $fd = fopen("php://stdin", "r");
    $email = "";
    while (!feof($fd)) {
    $email .= fread($fd, 1024);
    }
    fclose($fd);
    $fh = fopen('/home/logmails/email.txt','a');
    fwrite($fh, $email."\n-------------------------------------------------------\n\n");
    fclose($fh);
  6. chmod a+x /home/logmails/mail_eater.php
  7. touch /home/logmails/email.txt
  8. chmod a+r /home/logmails/email.txt
  9. Create a new transport using this file by appending the following line in master.cf:
    file_route unix - n n - - pipe user=logmails argv=/home/logmails/mail_eater.php
  10. Use this as the default transport in main.cf: default_transport = file_route
  11. Restart postfix ie. /etc/init.d/postfix restart
  12. Send a test email and then do a: cat /home/logmails/email.txt to see the emails

Take from:

Take from: https://serverfault.com/questions/451240/how-do-i-set-up-postfix-to-store-e-mail-in-a-file-instead-of-relaying-it

Leave a Reply