Fetchmail on DreamHost

Fetchmail is an excellent mail retrieval utility. The original purpose was to fetch mail from a remote server and deliver them to a local mail transfer agent, such as sendmail. This was mainly used for allowing systems that weren’t always online to still act as their own mail server. Thanks to the flexibility of Fetchmail, we can also take those messages and send them to any command.

While I wrote this tutorial to cover my Fetchmail setup in a DreamHost shared hosting account, most of it can be applied to any Linux server. However, if you have root access to the server you would be better off installing Fetchmail from your distribution’s repositories rather than manually compiling it from source.

Preparation

The first thing you’ll need to do is create a directory to hold the source. Then you can download and extract it.

# create a directory for the source and change to it
mkdir ~/src && cd ~/src

# get the source
wget http://downloads.sourceforge.net/fetchmail/fetchmail-6.3.26.tar.xz
tar xvf fetchmail-6.3.26.tar.xz
cd fetchmail-6.3.26

Installation

Now that you have the source downloaded and extracted, you’re ready to get fetchmail installed.

./configure --prefix=$HOME/opt/fetchmail-6.3.26
make
make install

Finally, since we’ve installed Fetchmail to a version specific directory, create a symlink that you can use to track your current version.

ln -s ~/opt/fetchmail-6.3.26 ~/opt/fetchmail

Configuration

Create your local fetchmail configuration file, ~/.fetchmailrc. An example configuration is below. You’ll obviously need to update the mail server, username, password, and command.

defaults
    protocol pop3

poll YOUR_MAIL_SERVER
    user "YOUR_EMAIL_USERNAME"
    pass "YOUR_EMAIL_PASSWORD"
    mda "COMMAND_TO_RUN"

Once your configuration is in place, you can perform a one time check by running $HOME/opt/fetchmail/bin/fetchmail. If that displays any errors, you’ll need to correct them before proceeding.

Schedule the Job

With Fetchmail installed and configured, all that’s left is to schedule it to run! We’ll use cron to do that. I generally schedule it to run every 5 minutes.

# append fetchmail to any existing cron entries

(crontab -l; crontab -l | grep -q "fetchmail" || echo "*/5 * * * * $HOME/opt/fetchmail/bin/fetchmail") | crontab -

Conclusion

If you’ve completed all the above steps, you should now have Fetchmail running on your shared DreamHost account. To ensure you receive any errors from the cron job, you may want to create a ~/.forward file for your account. The only contents should be a valid email address to reach you. By doing this, any messages going to your shell account will instead forward to the email address you specified.

If you have any questions or issues, or would like to suggest a topic, please reach out via the Contact link at the top of the page.