This is how to get postfix mail transfer agent to log emails to a file instead of sending to the outside SMTP world.
- Create a user that will own the e-mail logs (or use an existing one). I called mine "logmails".
useradd -c "Email Log" -d /home/logmails logmails
mkdir /home/logmails
- 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): #!/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);
chmod a+x /home/logmails/mail_eater.php
touch /home/logmails/email.txt
chmod a+r /home/logmails/email.txt
- 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
- Use this as the default transport in
main.cf
:default_transport = file_route
- Restart postfix ie.
/etc/init.d/postfix restart
- Send a test email and then do a:
cat /home/logmails/email.txt
to see the emails
Take from: