Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] User manageable mail forwarding procmail recipe
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Unsupported Software
View previous topic :: View next topic  
Author Message
93nt00r0ck5
n00b
n00b


Joined: 17 Jul 2004
Posts: 35

PostPosted: Fri Nov 21, 2008 3:28 pm    Post subject: [SOLVED] User manageable mail forwarding procmail recipe Reply with quote

I am trying to find ways to make our email system more robust and user friendly (in the easiest way possible). I have found an awesome procmail recipie that automates user vacation auto replies (http://www.opensourcehowto.org/uploads/scripts/vacation.txt). This is a godsend since I no longer have to run the vacation script manually anymore for each and every user that takes 1/2 of a day off; and then try to remember to remove the .vacation files the next day. This script allows them to send an email to themself to turn on an out-of-office reply with their own message. It *can* be password protected and they can turn it off themself. Pretty dang handy if you ask me.

Now I would like to do the same thing to automate their mail forwarding. Because, you know, if they are out of the office, they want someone to check their email for them. I figured I could modify the vacation script to fit my needs, but it soon became apparent that I don't know a thing about procmail and/or formail and/or sed. Idealy, I would just like to have another procmail recipie that would handle this. Here are a few of my requirements:

1) HAS to be password protected. (If someone could turn on someone else's forwarding, that would be a security breach.)
2) Original email sent to original recipient as well as the user/s chosen to be forwarded to.
3) It would be cool of you could append the body of the message saying this mail was forwarded to: xxx@mydomain.com, etc. So that the original recipient knows that particular email was forwarded while they were out-of-the office.
4) Ability to forward to multiple email address either fully qualified or just the user name. (eg. www xxx yyy@mydomain.com zzz@myotherdomian.com)
5) NEEDS to be independant of the vacation out-of-office. I have a couple of users that like to have their mail forwarded even when they are IN the office; go figure.

Here is the basic outline of how I think it should work. The user sends an email to themself with the subject: foward mail to xxx@mydomain.com password: mypassword. Procmail/Formail would process this subject line, check the password, extract the email address and put it in a file (something like mail.forward; not the .forward file) and then the next time that user gets an email, it would check to see if the forward file exists, then send the message to the original user with the modified body from #3 above, and forward (or better yet, redirect) the email to xxx@mydomain.com.

When the user no longer wants their email forwarded, they would send an email to themself with the subject: forward off password: mypassword. Procmail would process this subject, check the password, and remove the forwarding file mail.forward. (That is about it. I may have oversimplified it and/or missed a step, but you get the basic gist.)

Sounds simple enough, right? Can anyone help me build the procmail scipt to do this?

If not, any good simple solutions? I DON'T want to install mysql/postgres, anything that will modify postfix or have some *all-in-one*/everything in the kitchen sink that is a pain to administer solution.

Thanks, Andrew


Last edited by 93nt00r0ck5 on Mon Nov 24, 2008 4:46 pm; edited 1 time in total
Back to top
View user's profile Send private message
93nt00r0ck5
n00b
n00b


Joined: 17 Jul 2004
Posts: 35

PostPosted: Sun Nov 23, 2008 4:47 pm    Post subject: Reply with quote

Just a follow up. I have made a bit of progress. So far, I have a recipe that checks the subject for the phrase "forward mail" and checks the subject for the correct password. If both of these tests pass, it then extracts the email addresses from the subject line and puts them in a file called ~/.autofwd_to. I'll paste what I have so far below. If anyone sees anything that might need fixed or added, please feel free to let me know.

Thx, Andrew

UPDATE: I think I pretty much have it working fully now, although it could still use some tweaking AND lots of testing. I have it installed on my local machine, handling local mail. I will put the code below, but be warned, this may not be ready for prime time. If any changes are made to the code, I will make note of it and just update the code below instead of making a whole new posting.

Code:

SHELL=/bin/sh
PATH=/bin:/usr/bin
MAILDIR=/var/spool/mail
DEFAULT=$MAILDIR/$LOGNAME
TESTDIR=/home/$LOGNAME
MAILDIR=$TESTDIR
LOGFILE=$TESTDIR/Proctest.log
LOG="--- Logging for $LOGNAME, "

#Troubleshooting:
VERBOSE=yes
LOGABSTRACT=all

# AUTOFWD Variables
SUBJECT_LOCK=$TESTDIR/${SUBJECT_LOCK:-".subject$LOCKEXT"}
AUTOFWD_LOCK=$TESTDIR/${AUTOFWD_LOCK:-".autofwd$LOCKEXT"}

AUTOFWD_COOKIE=${AUTOFWD_COOKIE:-"forward mail"}
AUTOFWD_ON=${ACATION_ON:-"$AUTOFWD_COOKIE to"}
AUTOFWD_OFF=${ACATION_OFF:-"$AUTOFWD_COOKIE off"}

AUTOFWD_PASSWORD=YOURPASSWORD
AUTOFWD_TO=$TESTDIR/${AUTOFWD_TO:-".autofwd_to"}

DOMAIN_NAME=YOURDOMAIN.com
AUTOFWD_SENDER=$LOGNAME@$DOMAIN_NAME
GETTEXT="[      ]*\/[^  ].*"

#Get the sender's bare email address from the first "From" line
FROM_=`formail -c -I"Reply-To:" -rt -xTo: \
  | expand | sed -e 's/^[ ]*//g' -e 's/[ ]*$//g'`

#Get the original subject of the email
#Discard superfluous tabs and spaces
SUBJ_=`formail -xSubject: \
  | expand | sed -e 's/^[ ]*//g' -e 's/[ ]*$//g'`
FORWARDMAIL="`cat .autofwd_to`"
SENDMAIL=/usr/sbin/sendmail

# Turn on/off forward
:0
# Make sure that it is the correct user and domain
* ^TO_\/[-\.a-z_]+@
*$ ^From:.*$\MATCH
*$ ^From:.*$\DOMAIN_NAME
# And that the password variable is set
* ? test -n $AUTOFWD_PASSWORD
* $^Subject:.*${AUTOFWD_PASSWORD}
{

        # FORWARD ON
        # if subject line matches magic cookie for ON:
        :0
        * $^Subject:.*${AUTOFWD_ON}
        {
                # pull email address out of subject
                :0c:$AUTOFWD_LOCK
                * $^Subject:${GETTEXT}
                | echo $MATCH | perl -wne'while(/[\w\.\-]+@[\w\.\-]+\w+/g){print "$&\n"}'> .autofwd_to

                # add message to the body
                :0f
                | cat - ; \
                  echo; \
                  echo '---------- FORWARD -----------------'; \
                  echo 'Your email is now being forwarded to'; \
                  echo `cat .autofwd_to`
        }

        # FORWARD OFF
        # if subject line matches magic cookie for OFF:
        # delete the .autofwd_to file and notify
        :0f
        * $^Subject:.*${AUTOFWD_OFF}
        | cat -; \
                echo '---------- FORWARD -----------------'; \
                echo 'Removing mail forwarding: '; \
                rm -vf $AUTOFWD_TO; \
                echo ; \
                echo "Removed mail forwarding."
}

:0
* ! $^Subject:.*${AUTOFWD_ON}
* ! $^Subject:.*${AUTOFWD_OFF}
* ! $^Subject:.*${AUTOFWD_PASSWORD}
* ! ^X-Loop: ${AUTOFWD_SENDER}
{
        :0c: #Preserve a copy of the email
        $DEFAULT
        :0fwh #Adjust headers
        | formail -A"X-Loop: $AUTOFWD_SENDER" \
                  -A"X-From-Origin: ${FROM_}" \
                  -i"Subject: $SUBJ_ (fwd)"
        # Forward the email
        :0
        | formail -A"X-Loop: $AUTOFWD_SENDER" \
          -I"Subject: ${SUBJ_} [autofwd]" \
        | ${SENDMAIL} -f"${FROM_}" $FORWARDMAIL
}


Thanks goes out to the original author of the vacation script (which I borrowd greatly from and can be found here), and several awesome procmail websites, especially this one: Timo's procmail tips and recipies
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Unsupported Software All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum