Forwarding a copy of email from specific senders

So here's the situation: I've got a cool phone that can receive email. I don't want to give the address of the phone out, because I want it to be reserved for important email. But if someone on the list of important people emails me, I want a copy of the mail sent to my phone (with another copy delivered normally so I can read it on my computer at home, too).

It's a simple enough thing to do, but figuring out the procmail documentation gave me a headache, so I'm documenting it.

Here's the steps:

  1. Make sure procmail's installed on the server. "which procmail" at a shell prompt ought to tell you where it lives if it's available to you.
  2. Put the following into a file called .procmailrc in your home directory:
    # Preliminaries -- set up some special variables
    SHELL=/bin/sh
    LOGFILE=${HOME}/procmail.log
    LOG="--- Logging ${LOGFILE} for ${LOGNAME}, "
    
    # Get sender's bare email address because we'll need it later
    FROM_=`formail -c -x"From " \
    	| expand | sed -e 's/^[ ]*//g' -e 's/[ ]*$//g' \
    	| awk '{ print $1 }'`
    
    # Get the original subject of the email
    # discard superfluous tabs and spaces
    # on some systems -xSubject: has to be -x"Subject: "
    SUBJ_=`formail -c -xSubject: \
    	| expand \
    	| sed -e 's/  */ /g' \
    	| sed -e 's/^[ ]*//g' -e 's/[ ]*$//g'`
    
    # note that the 'c' in the :0 c below means that we're working on a copy
    # of the email. The original mail is still delivered normally.
    :0 c
    # is it from the magical list?
    * ^FROM.*(person1@domain1\.com|person2@domain2\.net)
    # avoid email loops
    * ! ^X-Loop: you@yourdomain\.com
    {
      :0 fwh	# Adjust headers before forwarding
      | formail -A"X-Loop: you@yourdomain.com" \
    	    -A"X-From-Origin: ${FROM_}" \
    	    -i"Subject: $SUBJ_ (fwd)"
      # forward the email to the other address
      :0
      # is it less than 64000 bytes in size?
      * < 64000
      # yes? Send it along
      ! you@otherdomain.com
    }
    # if not, toss it (remember, we're working on a copy)
    

    And that's most of the work. Note that the spaces are important in this.
  3. Put the following into a file call .forward in your home directory (you need the quotes):
    "|IFS=' ' && exec /usr/local/bin/procmail || exit 75 #your_username"
    

That should do it. Make sure to test both cases, an email coming from an important sender, and an email coming from someone not important. You don't want to be losing email.

Copyright 2009, Dave Polaschek. Last updated on Mon, 15 Feb 2010 14:09:33.