Postfix Auto-Reply Using Vacation
If you are looking for a simple way to setup Postfix Auto-Reply using vacation on any Linux, BSD or OSX system, look no further! This article assumes you have a working Postfix mail server up and running, as well as a basic understanding of the command line.
Vacation was developed by Eric Allman and the University of California, Berkeley in 1983. It was originally created to send a message to an email sender stating that the recipient is on vacation and not checking email. This article explains a similar approach, showing how to setup auto-replies for “no-replies” addresses used by automated email systems. I will use the domain “krisspencer.com” in all examples.
I currently have a website setup which automatically emails anyone that signs up for an account. The email is a simple, “Thank you for signing up” message, containing a few links to help guests get started. The email is sent to the guest from “email@example.com”. However, there are people that reply to these messages even though the address is a “no-replies” address. I need to let these people know that I may never receive their message, and provide them with alternative contact options.
First, check to see if you have vacation installed. Most likely you will have to do this as root.
# which vacation
If you get a response, then you have vacation. If you do not see a path to a copy of vacation, you should install it at this time. Depending on your package manager, the command will vary. Here are some example commands to install using different package managers:
# yum install vacation
# apt-get install vacation
# aptitude install vacation
Now that vacation is installed, make note of where it is installed by issuing a “which vacation”. Now add a user for vacation to run as. I recommend it be called “autoreply”. Configure this new user so that it cannot login and has a home directory of /home/autoreply. Next, make this new user the owner of the vacation binary using chown. Be sure to use the correct path to vacation.
# chown autoreply /usr/bin/vacation
With Postfix virtual domain support, you can configure multiple auto-replies. This article uses “firstname.lastname@example.org”.
Next, configure the vacation message and database files for “email@example.com”.
The message file (.msg) contains the message you want to be used as your auto-reply. It is a text file that contains the message and headers. Using your preferred text editor, create the file “vacation-no-replies-krisspencer.msg” in /home/autoreply. Make sure the user “autoreply” owns the new file.
# vi vacation-no-replies-krisspencer.msg
# chown autoreply vacation-no-replies-krisspencer.msg
The file should look something like this:
Subject: No Replies Auto Response
Sorry, this email address does not accept emails. If you would like to contact
Kris Spencer, please visit http://krisspencer.com/contact/
Kris Spencer’s Friendly Email System
Of course, you can say whatever you’d like in this message. We recommend it be something friendly and informative.
Now it is time to initialize the database (.db) file. This is a required file for vacation, used to keep track of who has already received an auto-reply. There is more than one way to create this file. It is likely that the .db file will be automatically created the first time that postfix runs vacation. If not, follow the recommended method below.
The standard method is to become the user “autoreply” and run the following command:
# vacation -I
This creates a file called vacation.db in /home/autoreply, for which you would have to rename. However, for security reasons, we have configured the user “autoreply” without a shell. Here is the recommended method to create the file as root:
# /usr/bin/vacation -a firstname.lastname@example.org -r 1 -f /home/autoreply/vacation-krisspencer.db -m /home/autoreply/vacation-no-replies-krisspencer.msg autoreply
Be sure you change “/usr/bin/vacation” to the exact path of where vacation is installed on your system. After executing this command, you will notice that it appears to do nothing and you never get your root prompt. Simply wait a few seconds and then hit CTRL-C (control-c) to return to your root prompt. If you do an “ls” of /home/autoreply, you should see the new file. This file needs to be owned by autoreply, so do the following:
# chown autoreply:autoreply vacation-krisspencer.db
Configuring Postfix Auto-Reply Using Vacation
Here is the ‘nuts and bolts’ portion of the article, where we configure Postfix to use our vacation configuration. File names may vary but generally follow the same conventions used below. You will do the following steps as root in the /etc/postfix directory.
1. Main.cf – Postfix configuration file
In your main.cf file, make sure the following lines exist. In most cases, you will find the first line exists but the second does not. Add lines as needed:
2. Virtual Aliases Map
Virtual Aliases mapping allows you to forward mail from one address to another, or one-to-multiple.
Below are two examples. Use example (a) if you with to not receive the incoming messages but send an auto-reply message. Use (b) if you wish to receive the incoming mail and send an auto-reply. You will put the contents of your chosen example into /etc/postfix/vmail_aliases.
email@example.com firstname.lastname@example.org, email@example.com
You will notice that example b references a non-existent hostname, “autoreply.krisspencer.com”. This is OK. We will map this in the next step.
IMPORTANT: After editing postfix map files, be sure to run the following command to build the .db database version of the file that postfix will read from:
# postmap vmail_aliases
3. Virtual Transport Map
Virtual Transport mapping allows you to map addresses and hosts to message delivery transports and nexxt-hop destinations. Message delivery transports are defined in master.cf. Create or edit /etc/postfix/vmail_transport and add the following line:
This maps any address to @autoreply.krisspencer.com to the autoreply transport, which we will define next. Make sure all your map files are owned by root, and just as before, remember to “postmap vmail_transport” so that your postfix server has a vmail_transport.db file to work with.
4. Master.cf – Postfix Transport Configuration file
Edit master.cf and add the new transport to the end of the file, as shown below. (This should end up being 2 lines)
autoreply unix – n n – 1 pipe
flags=F user=autoreply argv=/usr/bin/vacation -a firstname.lastname@example.org -f /home/autoreply/vacation-krisspencer.db -m /home/autoreply/vacation-no-replies-krisspencer.msg autoreply
This tells postfix to pipe input to the autoreply transport to vacation using the “krisspencer” .db and .msg files. Again, make sure you have the correct path to vacation on the required files defined.
You are done! The only thing left to do is restart postfix so that the changes are loaded into memory. Either of the following commands will do the trick:
# postfix reload
# postfix restart
In some cases, you may need to call the server’s init file using the full path:
# /etc/init.d/postfix restart
The great thing about using this method is that you can set up as many auto responders as you’d like. On a server with multiple domains, I’ll have .db, .msg and transport configured for each domain. In some cases, multiple auto-reponders for different addresses of a single domain. The key is to have a uniquely named transport associated to properly configured .db and .msg files, and the transport map correctly configured to use the unique transport name.