Procmail Tips: How to forward your email.

Not that you would ever want to forward your email away from Sonic.net, but maybe you like the idea of archiving and searching your E-Mail at a secondary E-Mail location; to that end the post below details how you should be forwarding your E-Mail in any UNIX environment.

You know that looping your mail is a bad idea; that is forwarding your email to Gmail and then forwarding that back to yourself, ad infinitum, you get the idea. It’s equally a bad idea to forward DSNs or bounce messages to Gmail (or anywhere really); as it causes the same sort of loop and the same kind of aggravation for your System Administrator. The following is a procmail recipe that works for me; just drop this into your .procmailrc in your home directory.

SENDMAIL=/usr/sbin/sendmail
FORMAIL=/usr/bin/formail

:0
* ^X-Loop: Your_Email_Address@sonic.net
/dev/null

:0E
* ^TO_Your_Email_Address+0x96rds@sonic.net
$DEFAULT

:0cE
| $FORMAIL -A “X-Loop: Your_Email_Address@sonic.net” | $SENDMAIL -f Your_Email_Address+0x96rds@sonic.net -oi Your_Email_Address@gmail.com

In detail:

SENDMAIL=/usr/sbin/sendmail
FORMAIL=/usr/bin/formail

Sets up some variables.

:0
* ^X-Loop: Your_Email_Address@sonic.net
/dev/null

Discards the message if it has the “X-Loop” header in it; this stops circular mail loops; for example if you forwarded Your_Email_Address@sonic.net to Your_Email_Address@gmail.com and then forwarded that back to Your_Email_Address@sonic.net.

:0E
* ^TO_Your_Email_Address+0x96rds@sonic.net
$DEFAULT

Places the message in your Inbox and does not forward it on if the “To” header contains our address plus the special string. This stops you from forwarding those nasty bounce messages to the server that just sent you the bounce, thus causing a ping pong affect.

Little known fact: your_user_name+anything@sonic.net will go to your_user_name@sonic.net, so you can utilize that little “+anything” to do special filtering like in this script.

:0cE
| $FORMAIL -A “X-Loop: Your_Email_Address@sonic.net” | $SENDMAIL -f Your_Email_Address+0x96rds@sonic.net -oi Your_Email_Address@gmail.com

Finally this bit adds the above conditions to your message before forwarding it on to your “other” E-Mail provider.