How to setup an Out of Office / Vacation reply using Postfix Mailserver & Procmail on Linux
Recently, a client of mine needed to setup an “Out of Office”, or also known as “Vacation” reply on his email for when he traveled. Since they don’t use Microsoft Exchange, MS Outlook’s Out of Office feature didn’t work.
The only way to setup an Out of Office reply, is on the mail server itself. Since you’re PC will be off when you’re away, it won’t work on your PC, cause your PC actually needs to download the email before replying with an out of office reply.
So, now we need to setup Procmail on Postfix, on our Linux mail server, to automatically reply to all emails sent to a certain user on the domain, let’ call him Bob. Bob’s email is bob@somedomain.com – so if you were to email bob@somedomain.com while he was out of town, you wouldn’t know it, unless he’s set his out of office reply.
For this to work, you need to have Procmail installed on your server. Procmail is normally installed on systems with postfix, but incase it’s not, follow your distributions installation instructions on how to install procmail.
On any yum base system, the following command should work:
[shell]
yum install procmail
[/shell]
alternatively, if you use apt, this will work:
[shell]
apt-get install procmail
[/shell]
For further info, see the Procmail Homepage. Also, take a look at the following howto: Filtering E-mail With Procmail
Now, let’s get cracking!
1. First, we need to enable procmail in Postfix, as follows:
[shell]
vi /etc/postfix/main.cf
[/shell]
P.S. If you don’t know, or don’t like vi, you can use pico / nano / emacs, or any other editor you like.
Now, make / add the following changes:
[shell]
mailbox_command = /usr/bin/procmail
[/shell]
and then save the file, and restart postfix, normally with /etc/init.d/postfix restart
2. Great! Now you need to add the following info to the user’s .procmailrc file, in their home directories:
[php]
# Uncomment the lines below if you need log output for testing.
#
#LOGFILE=/tmp/procmailvacation.log
#VERBOSE=on
# vim: ft=procmail
# User-managed vacation recipe for procmail
# Written by Jason Thaxter
# (http://www.google.com/search?q=jason+thaxter)
# * Include this file in the procmail file.
# * Set $VACATION_PASSWORD. (for security, this is mandatory)
# * Define $VACATION_SENDER in your procmail recipe: it will be “from” this
# address.
# * E-mail a message with $VACATION_PASSWORD and $VACATION_ON in the subject
# line. The body of the message becomes the vacation message. $VACATION_ON
# can be set prior to the INCLUDERC, but it defaults to “vacation on”.
# * To turn it off, e-mail a message with $VACATION_PASSWORD and $VACATION_OFF
# in the subject line. Likewise, $VACATION_OFF defaults to “vacation off”.
# Note that you probably want this to execute *after* any mailing list or spam
# delivery recipes. You can set $VACATION_SKIP to disable vacation processing
# if it’s inconvenient to skip this recipe.
# —————————————————————————–
# Configurable variables: These variables allow you to use this vacation recipe
# as an include and customize it from your main procmail file.
#
# lockfile:
VACATION_LOCK=$HOME/${VACATION_LOCK:-”.vacation$LOCKEXT”}
# cache file:
VACATION_CACHE=$HOME/${VACATION_CACHE:-”.vacation_cache”}
# cache size:
VACATION_CACHE_SZ=${VACATION_CACHE_SZ:-8192}
# message file
VACATION_MSG=$HOME/${VACATION_MSG:-”.vacation_mesg”}
# what to use as the xloop header
HOSTNAME=${HOSTNAME:-`hostname`}
VACATION_XLOOP=${VACATION_XLOOP:-”$LOGNAME@$HOSTNAME”}
# base token for default $VACATION_ON and $VACATION_OFF
# so you could set this and not those individually
VACATION_COOKIE=${VACATION_COOKIE:-”vacation”}
VACATION_ON=${VACATION_ON:-”$VACATION_COOKIE on”}
VACATION_OFF=${VACATION_OFF:-”$VACATION_COOKIE off”}
#
#Change these variables
#
VACATION_PASSWORD=yourpassword
VACATION_DOMAIN_NAME=domainname.com
VACATION_SENDER=$LOGNAME@$VACATION_DOMAIN_NAME
VACATION_SENDMAILFROM=${VACATION_SENDMAILFROM:-”-f$VACATION_SENDER”}
VACATION_SENDMAILFLAGS=”-oi -t $VACATION_SENDMAILFROM”
# —————————————————————————–
SENDMAIL_CMD=”$SENDMAIL $VACATION_SENDMAILFLAGS”
SHELL=/bin/sh
# check if we should send vacation message, add user to cache
:0 Whc: $VACATION_LOCK
# if i haven’t been instructed to skip processing
* ? test -z $VACATION_SKIP
# if i have a vacation message file
* ? test -f $VACATION_MSG
# and the message is not from a daemon or mailer
* !^FROM_DAEMON
* !^FROM_MAILER
# not declared spam by spamassassin
* !^X-Spam-Flag: YES
# not discernably in a mailing list
* !^List-
* !^(Mailing-List|Approved-By|BestServHost|Resent-(Message-ID|Sender)):
* !^X-[^:]*-List:
* !^X-(Sent-To|(Listprocessor|Mailman)-Version):
# and not x-loop
* !^X-Loop: $VACATION_XLOOP
# add it to the cache
| formail -rD $VACATION_CACHE_SZ $VACATION_CACHE
:0 ehc
# if the name was not in the cache
# if we can find who we’re sending it to
# and who we are sending this “From”
* ? test -n ${VACATION_MSG_SEND_TO}
* ? test -n ${VACATION_SENDER}
*$ !^From:.*$VACATION_SENDER
| (formail -r \
-I”Precedence: junk” \
-A”From: $VACATION_SENDER” \
-A”X-Loop: $VACATION_XLOOP”; \
cat $VACATION_MSG ) | \
$SENDMAIL_CMD
# Add/remove vacation message
:0
# First make sure that the sender has
# the correct username
* ^TO_\/[-\.a-z_]+@
*$ ^From:.*$\MATCH
# the correct email domain
*$ ^From:.*$\VACATION_DOMAIN_NAME
# only do this if we have a password set
* ? test -n $VACATION_PASSWORD
# and it’s in the subject line
* $^Subject:.*${VACATION_PASSWORD}
{
# VACATION ON
# if subject line matches magic cookie for ON:
:0
* $^Subject:.*${VACATION_ON}
{
# pipe the body into the vacation message file
:0c:$VACATION_LOCK
| formail -I “” > $VACATION_MSG
# add message to the body
:0f
| cat – ; \
echo; \
echo ‘———- VACATION —————–’; \
echo ‘The above text was installed as your vacation message’
}
# VACATION OFF
# if subject line matches magic cookie for OFF:
# delete the vacation file and notify
:0f
* $^Subject:.*${VACATION_OFF}
| cat -; \
echo ‘———- VACATION —————–’; \
echo ‘Removing message and cache: ‘; \
rm -vf $VACATION_MSG; \
rm -vf $VACATION_CACHE; \
echo ; \
echo “Removed vacation message.”
}
[/php]
Tip: If you want this file to be added to all new users on your system automatically, then you need to add this info to the following file as well:
/etc/skel/.procmailrc
We’re almost there…..
4. Once the .procmailrc file has been added / updated, we need to edit it. Each user can effectively edit his own file, but it’s not recommended.
The following changes needs to be made:
$VACATION_PASSWORD - this is to prevent others from enabling/disabling your vacation auto-reply. Let’s make the password mypasswordfor now
$VACATION_DOMAIN_NAME - this is the domain name used for your emails (e.g. user@somedomain.com ).
Lastly, you need to make sure that the .procmailrc file has the following security permissions:
User and Group should be set to the username of the email user you’re setting it up for.
File Permissions should be set to rwx—— for the user only (i.e. 0700).
And that’s it with regards to setting it up. You can now use it
5. To enable the vacation messsage, Bob (bob@somedomain.com) needs to send himself an email, with the following email subject:
mypassword vacation on – Just to clarify, it’s the password that was set in the .procmailrc file (which is “mypassword”), followed by the word “vacation”, followed by “on”.
To disable the vacation mode, Bob needs to send himself an email again, but with the following email subject:
mypassword vacation off
Here’s the original source for the vacation file