fake procmail script

This is similar to fake sendmail. Need a procmail script to deliver messages locally on your machine? Need to do postfix development local for mail ? This will override the “to” field on postfix (more or less) by saving all mail to a local file. Here is a PHP script you can use:

Tweak it to fit your needs, adjust #!/usr/bin/php to point to the php file that you use on your system if necessary ( try which php )

The script may need to be in a folder that it has write access to, or that postfix / procmail has write access to.

After saving the script to something like /usr/local/fake-procmail.sh edit /etc/postfix/main.cf and add or edit this line:

mailbox_command = /usr/local/fake-procmail.sh

Also make sure you make the script executable:

chmod +x /usr/local/fake-procmail.sh

You will also need to route all mail to the localhost by adding these lines:

luser_relay = [email protected]
local_recipient_maps =
mydestination = pcre:/etc/postfix/mydestinations

Change johndoe to your user name.

Also create a file /etc/postfix/mydestinations and add this to it:

/.*/         ACCEPT

More info about that here.

Then restart postfix if needed:

sudo /usr/sbin/postfix stop

sudo /usr/sbin/postfix start

And try sending a message, or piping some text to the script:

echo hello there | mail -s "test message" [email protected]

Or

echo hello there | /usr/local/fake-procmail.sh

There should now be messages in /tmp/procmail-messages/ – change the script to change the location where they are saved.

It’s easier than setting up and chrooting postfix on Mac OS X Lion / Mountain Lion ( mac osx postfix override ) – for local testing – because after that upgrade postfix was often reported broken by many users – with the directory /Library/Server/Mail/Data/spool having gone missing and reporting sendmail: fatal: chdir /Library/Server/Mail/Data/spool: No such file or directory ( to fix that error see below the script )

#!/usr/bin/php
< ?php

function getInput(){
        if(empty($_SERVER['SHELL'])){
                return;
        }
        $r = '';
        $f = fopen('php://stdin', 'r');
        while ($line = fgets($f)){
           $r .= $line;
        }

        fclose($f);
        return $r;

}

$r = getInput();

$micro = explode(' ',microtime());
$micro = array_shift($micro);
$dir = '/tmp/procmail-messages/';
if(!is_dir($dir))
{
mkdir($dir,0777);
}
$f = $dir.date('y-m-d_h_i_s').$micro.'.eml';

file_put_contents($f,$r);

Finally, if you get the error like this in /var/log/mail.log

postscreen[21354]: CONNECT from [127.0.0.1]:56145
postscreen[21354]: WHITELISTED [127.0.0.1]:56145
smtpd[21359]: fatal: open /etc/postfix/submit.cred: No such file or directory
master[21176]: warning: process /usr/libexec/postfix/smtpd pid 21359 exit status 1
master[21176]: warning: /usr/libexec/postfix/smtpd: bad command startup -- throttling

You may need to create the /etc/postfix/submit.cred file

OS X 10.8 Mountain Lion: I get an error when trying to run sendmail

From Sendmail error on osx lion:

sudo mkdir -p /Library/Server/Mail/Data/spool
sudo gzip /usr/share/man/man1/{postalias.1,postcat.1,postconf.1,postdrop.1,postfix.1,postkick.1,postlock.1,postlog.1,postmap.1,postmulti.1,postqueue.1,postsuper.1,sendmail.1}
sudo gzip /usr/share/man/man5/{access.5,aliases.5,bounce.5,canonical.5,cidr_table.5,generic.5,header_checks.5,ldap_table.5,master.5,mysql_table.5,nisplus_table.5,pcre_table.5,pgsql_table.5,postconf.5,postfix-wrapper.5,regexp_table.5,relocated.5,tcp_table.5,transport.5,virtual.5}
sudo gzip /usr/share/man/man8/{anvil.8,bounce.8,cleanup.8,discard.8,error.8,flush.8,local.8,master.8,oqmgr.8,pickup.8,pipe.8,proxymap.8,qmgr.8,qmqpd.8,scache.8,showq.8,smtp.8,smtpd.8,spawn.8,tlsmgr.8,trivial-rewrite.8,verify.8,virtual.8}
sudo /usr/sbin/postfix set-permissions
sudo chmod 700 /Library/Server/Mail/Data/mta
sudo /usr/sbin/postfix start

Another option, if you want a nice GUI, is to test out FakeSMTP to replace postfix / procmail altogether:

fake procmail script

Related Posts:

  • No Related Posts
This entry was posted in Tech Tips, Web Development and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *