APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

© December 2007 Anthony Lawrence

Maccy Xmas and hope you have some time off (procmail vacation by James Cetrangelo)

I happened to win an iPod in a contest and, not having any use for it myself, I decided to give it to a niece. She's only 8, so I'm pretty sure she doesn't read this site; I'm not giving away the surprise by mentioning it here..

Are you hoping for a Maccy Christmas? Hanukkah was early this year, so maybe you are already fondling that iPhone or MacBook Pro.. I hope so.

As for me, I'm planning on taking a few days off, and that is what leads me into the subject of something I wrote about some time ago: a smarter email vacation manager.

I never got around to that, but James Cetrangelo did - he created a procmail recipe that implemented the ideas. You can find it at https://www.blackfeathermedia.com/code/vacation_manager.php, and I've also listed it here for your admiration and enjoyment:

# Email-based vacation autoresponder system
#   Written by James Cetrangelo <james@blackfeathermedia.com> (noob)
#   based on an idea from Tony Lawrence (https://aplawrence.com)
# This code is free to distribute, change, publish, or sell.  Just keep
# my name & email somewhere in the comments, and this paragraph intact.
# If you like it, hate it, or make improvements, please let me know.
# Use at your own risk.  I'm not responsible if your mail gets eaten.
# Check https://www.blackfeathermedia.com for updates.
# Change variables on top to suit your system.  The recipies are not 100%
# portable since it uses 'test -e' to check for existance of files.  This
# can probably be optimized and cleaned up by someone with more experience.
# Needless to say, it's not for fellow noobs.  You should know what you're
# doing before you let my rogue code near your email.  It could also use a 
# more substantial spam filter to prevent autoreplies to address farmers.
# I know some of my shell calls can be optomised to use internal procmail
# functions.  A good resource: https://pm-doc.sourceforge.net/pm-tips.html
#   - Change variables at top to match your system settings
#   - Rename to .procmailrc and add to your home directory
#   - Do NOT create 'vacation_message.txt' or header files
#       (required files will be created or deleted when needed)
#   - Send an email to yourself with 'Vacation On' in the subject.  The body
#       of this message will become your outgoing message.
#   - The system will save that message to local files, and email you back
#       with a random verification code.
#   - Reply to that confirmation message to enable the vacation autoresponder.
#   - Any incoming email will recieve your outgoing message as a reply.
#       This should exclude obvious spam (if Spam-Asassin is running) and
#       repeat messages (one reply per email address)
#   - Disable the autoresponder by sending an email to yourself with 
#       'Vacation Off' in the subject line.  It can also be disabled by
#       simply deleting the 'vacation_*.txt' files created.



# Leave unused alias names below set to $USERNAME.
# set it to "sales" or "support" when these are forwarded to you

# When user sends an email to themselves with 'vacation on' in the subject,
# their new outgoing message is temporarily stored, and a verification code
# is sent in a confirmation email.  Outgoing messages can be any mime type.
* $ ^From.*$\USEREMAIL
* H ?? ^Subject:.*Vacation On
* $ !^X-Loop: $\USEREMAIL
  CODE= `date | md5sum`
  CODE= `expr substr "$CODE" 3 10`

  :0 hcW: vacheader.lock
  | formail -X Content-type | cat > $VACHEADER.tmp

  :0 bcW: vacmessage.lock
  | cat > $VACMESSAGE.tmp
  :0 cWi: vaccode.lock
  | echo "$CODE" > $VACCODE

  | (formail -r -I"From: $USEREMAIL" -A"X-Loop: $USEREMAIL"
  -I"Subject: Vacation Autoresponder Message Recieved"; \
    echo -e "This is an automatic message from your vacation autoresponder: \n" \
            "\nYour new outgoing message has been saved.  To enable vacation mode," \
            "simply reply to this message.  The code below is used to verify" \
            "your email address.  Vacation mode wont be enabled until you reply. \n" \
            "\nVerification Code: $CODE\n") | $SENDMAIL -oi -t

# When the user replies to the above confirmation email containing the code,
# Vacation mode is enabled: Filenames changed, and a cofirmation message is sent
* $ ^From.*$\USEREMAIL
*   ^Subject:.*Vacation Autoresponder Message Recieved
* B ?? ^Verification Code: \/.+
* $ !^X-Loop: $\USEREMAIL
* $ ? test -e $VACCODE
* $ ? test -e $VACMESSAGE.tmp
* $ ? test -e $VACHEADER.tmp
  CODE= `cat $VACCODE`

    :0 ci: vacmessage.lock

    :0 a
    | (formail -r -I"From: $USEREMAIL" -A"X-Loop: $USEREMAIL"
    -I"Subject: Vacation Autoresponder Enabled"; \
      echo -e "This is an automatic message from your vacation autoresponder: \n" \
              "\nVacation mode is now enabled.  All incoming email will automatically" \
              "recieve your outgoing reply.  To turn off the vacation autoresponder," \
              "simply reply to this message, or send yourself an email with 'Vacation Off'" \
              "in the subject line.") | $SENDMAIL -oi -t

# If the user replies to the above message, or sends an email to themselves
# with "Vacation Off" in the subject line, then vacation mode is disabled.
# Outgoing message is deleted, and a confirmation message is sent.
* $ ^From.*$\USEREMAIL
* ^Subject:.*Vacation (Off|Autoresponder Enabled)
* $ !^X-Loop: $\USEREMAIL
  :0 ci: vaccache.lock
  * $ ? test -e $VACCACHE
  | rm $VACCACHE

  :0 ci: vacmessage.lock

  :0 a
  | (formail -r -I"From: $USEREMAIL" -A"X-Loop: $USEREMAIL"
  -I"Subject: Vacation Autoresponder Disabled"; \
    echo -e "This is an automatic message from your vacation autoresponder: \n" \
            "\nVacation mode is now off.  Incoming emails will no longer" \
            "recieve your vacation message.") | $SENDMAIL -oi -t

# If the vacation message and header files exist, vacation mode is on.
# Any incoming mail gets checked against a list.  New email addresses are
# added to the list, and the outgoing message is sent to them.  If Spam-Asassin
# is installed, the most obvious spam will be excluded (but not filtered).
:0 Whc: vaccache.lock
* $ ^To:.*\<($\USERNAME|$\ALIAS1|$\ALIAS2|$\ALIAS3)\>
*  !^X-Spam-Flag:.*YES
* $!^X-Loop: $\USEREMAIL
* $ ? test -e $VACHEADER
* $ ? test -e $VACMESSAGE
#   ^Subject:.*Vacation Test
| formail -rD 8192 $VACCACHE

:0 ehc
| formail -r -I"From: $USEREMAIL" -A"X-Loop: $USEREMAIL" | cat -
$VACHEADER | tr -s '\n' | cat - $VACMESSAGE | $SENDMAIL -oi -t

Nice job, James! Thank you for sharing!

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Maccy Xmas and hope you have some time off (procmail vacation)

Inexpensive and informative Apple related e-books:

Take Control of Automating Your Mac

iOS 10: A Take Control Crash Course

Take Control of iCloud, Fifth Edition

Take Control of the Mac Command Line with Terminal, Second Edition

Take Control of IOS 11

More Articles by © Anthony Lawrence

Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Printer Friendly Version

The activity of "debugging", or removing bugs from a program, ends when people get tired of doing it, not when the bugs are removed. (Datamation)

Linux posts

Troubleshooting posts

This post tagged:




Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode