HowTos/Fetchmail

From Scalix Wiki
Jump to: navigation, search

Scalix Wiki -> How-Tos -> Fetchmail

Fetchmail

Fetchmail is a program used to relay messages from the server to which they were addressed to another destination, such as a local server, without the message actually containing routing information to that final destination.

You can get fetchmail from http://fetchmail.berlios.de/. Your linux distribution almost certainly provides finished fetchmail packages as well, which you can install with yast, yum, up2date, apt-get or another program depending on your distribution. Refer to your linux system's documentation for further information on installing packages.

Once it is installed, fetchmail can either retrieve mail from specific mailboxes and deliver it to specific mailboxes, in what is called singledrop mode, or it can retrieve messages from one source mailbox and deliver them to multiple recipient mailboxes, in what is called multidrop mode. In this example we will configure single drop mode to retrieve messages from, and deliver them to, a single user.

There are two ways to configure fetchmail, the first is with a configuration file, the second is with parameters on the command line. We will use the command line in this example, although a configuration file could do the same job equally well.

Set Up Fetchmail

In order to retrieve and redeliver email fetchmail will need to know where messages are coming from and where they are going.

Let's say your messages are cached by an email provider, and you want to pick them up at regular intervals and put them in your Scalix inbox. You could use a fetchmail command something like this:

fetchmail -p pop3 -S yourserver.localdomain.com --smtpname your.name@localdomain.com -u youruser@provider.com popserver.provider.com

This command would retrieve all messages belonging to youruser@provider.com from your provider's server located at popserver.provider.com and deliver them to the your.name@localdomain.com mailbox hosted on the Scalix server yourserver.localdomain.com

Take special notice of the -S flag, and its value yourserver.localdomain.com. If you are running fetchmail directly on your mailserver, you might want to use localhost or 127.0.0.1 as the address to which fetchmail will deliver the messages it gets from your ISP. This will not work, because sendmail listens to localhost:25 in order to accept outgoing mail sent from the Scalix server for delivery. Incoming mail from the outside to the Scalix server is recieved on port 25 on the official server ip-addr. That is the ip you get by executing the "host `hostname --fqdn`" command. The safest and easiest thing to do is always to use the server's fully qualified domain name.

If you want to leave the messages on your provider's server, and just copy them to your local server, add the -k flag to this command.

Automating Fetchmail

You can use the cron program to retrieve your messages on a regular basis.

Refer to chapter 9.4 of http://www.tldp.org/LDP/lame/LAME/linux-admin-made-easy/index.html for a thorough description of the cron daemon.

You can use a variant of

*/5 * * * * "/usr/bin/fetchmail -p pop3 -S yourserver.localdomain.com --smtpname your.name@localdomain.com \
 -u youruser@provider.com popserver.provider.com >/dev/null 2>&1"

to cause fetchmail to retrieve and deliver mail every five minutes.

Rinse and repeat for each additional user

Dealing with Passwords

In order to get your pop3 email, you will need to authenticate yourself to your email server. Normally this entails providing a password. Fetchmail will not accept the password on the command line, so you need to put it in a file. There are good security reasons for this behavior: /proc/$PID/cmdline is world-readable for example.

Fetchmail will, however, read the password from a file. This file should be mode 400 so it's only readable by its owner. The user running fetchmail should create a ~/.netrc file to hold the password. The format of the file is:

machine popserver.provider.com
login youruser
password pop3password

Fetchmail reads this file at runtime to find the appropriate password. If multiple users are having their mail processed, entries can be stacked in the .netrc file to include login data for several machines.

Alternatively, if your fetchmail configuration is stored in a fetchmailrc file, you can store your password there. This file should of course also be mode 400.