Fetchmail

From Wiki99

Revision as of 00:23, 8 November 2007; view current revision
←Older revision | Newer revision→
Jump to: navigation, search

↑ Computers ↑
← prev: Mail SSL Setup next: SendEmail →


Contents

Fetchmail

After all the complexities of the above, it is a relief to deal with a much simpler part of the problem, namely fetchmail.

Here's the problem fetchmail solves:
Right now you have your own domain name, and mail sent to your domain name will be caught by postfix, picked up by IMAP, and will appear in Mail.app. But you probably have at least one old email address, for example fred1@comcast.net. You can continue to receive that mail through Mail.app the way you used to, if you like, but you won't get any of the benefits of having an IMAP server and a consistent view of that mail across multiple computers. So how do you fix this?
The solution is fetchmail, which is a program that runs on your server, usually sleeping. Every few minutes it wakes up, contacts a list of email addresses you have told it about, downloads all the mail from each email address, and forwards that mail on to a different email address. What we will obviously do is tell it to forward that mail on to our domain name address.

Setting up fetchmail is pretty easy. The program comes as part of OS X, so we don't have to download anything.

launchd setup

What we do have to do first is create a launchd item that will ensure that whenever we reboot our computer, fetchmail starts up automatically. Nothing new here, we've seen this before for dnsupdate and postfix. On your server create the file /Library/LaunchDaemons/fetchmail.plist and type in

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>GroupName</key>
        <string>wheel</string>
        <key>Label</key>
        <string>fetchmail</string>
        <key>LowPriorityIO</key>
        <true/>
        <key>Nice</key>
        <integer>1</integer>
        <key>OnDemand</key>
        <false/>
        <key>Program</key>
        <string>/usr/bin/fetchmail</string>
        <key>ProgramArguments</key>
        <array>
                <string>/usr/bin/fetchmail</string>
                <string>-f</string>
                <string>/var/root/.fetchmailrc</string>
                <string>-L</string>
                <string>/var/log/mail.log</string>
                <string>-s</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>UserName</key>
        <string>root</string>
</dict>
</plist>

.fetchmailrc config file setup

As you can probably guess if you read through the plist file above, fetchmail will read its setup configuration from the file /var/root/.fetchmailrc.

The prefs setup in UNIX may seem a little spread all over the place, but there is a vague sort of logic to it. For preferences that are determined for the whole computer, the preferences live in /etc, and we have seen some of these already, for example /etc/hostconfig and the files in /etc/xinetd.d.

You might think it would make sense to also put the fetchmail preference file in /etc; and this would not be totally stupid.

However the convention that seems to have arisen is the idea that fetchmail is not an essential part of the operating system, like the programs we've worked with so far, but is more just a convenience that most users will run in their own accounts if they want to. So fetchmail's preferences are considered to be a "per-user" concept, not a system wide concept.

We, however, are doing something a little unusual, running fetchmail system-wide, not on behalf of a particular user. This means that fetchmail runs as the root user, and so its prefs file should be in the home directory of root. What is the home directory of root? To the extent that this concept makes sense, it is usually taken to be /var/root.

We now need to write the configuration file, /var/root/.fetchmailrc, which tells fetchmail which email address to fetch mail from. You can, if you want, type man fetchmail to get the full gory details of fetchmail's prefs file syntax, but it is much easier to start with a working file and just modify the pieces you want.

So here's a basic working .fetchmailrc

set syslog
set daemon 60

poll pophost.yourisp.com protocol pop3 :
user remotelogin with password remotepassword is locallogin here
# keep

Note that only about half the words in file are actually meaningful to fetchmail. The others are there (along with the splitting of the last long line across multiple lines) just to make the file a little easier to understand, and are ignored by fetchmail.

The first line tells fetchmail to store in the system log (which you can view using Console.app) anything important that happens while it runs.
The second line tells fetchmail to wake up and look for new mail every 60 seconds. You can change this to say 300, to check for mail every five minutes, if you prefer

The last line (formatted to run over three lines of text) is the important line. You have one line like this for each email account you want to fetch mail from. In the first line, change

pophost.yourisp.com

to the pop server you use for your ISP, eg pop.comcast.net. In the next line change

remotelogin

to whatever user name you use with that isp, for example fred27, change

remotepassword

to whatever password you use to read mail from that isp, eg blUF6s, and change

locallogin

to your account name on your server, eg fred.

Finally, while you are testing, you should add the word keep to the end of the line. This will download the email through fetchmail, but will still leave it on your ISP's POP server so you can read it the way you always used to. Once you are done testing and, in a few days, are convinced that everything is working properly, you can remove or comment out (using #) the keep line.

You can add additional accounts to poll below that first account.

So you should now have a file that looks something like

set syslog
set daemon 60

poll pop.comcast.net protocol pop3:
user fred1 with password blUF6s is fred here
keep

poll pop.sbcglobal.yahoo.com protocol pop3:
user "fredx18@sbcglobal.net" with password HHj67k is fred here
keep

Set the appropriate permissions for your .fetchmailrc file by typing

sudo chmod 0710 /var/root/.fetchmailrc

Testing that it works

Now you can test things. Reboot the server. Open Mail.app, and send a message to yourself at your ISP address, ie one of the addresses you added to your .fetchmailrc above. Wait a minute or two, tell Mail.app to Get New Mail, and you should see two copies of your email

  • one in your IMAP account, because it was pulled in by fetchmail, and
  • one in your old ISP account, because fetchmail is leaving mail it fetched from the POP server on the server, where other clients can still see it.

In a few days, once you are happy with your new mailsetup and feel it is reliable, you can remove the keep line from each poll section of your /var/root/.fetchmailrc file, and remove or deactivate your ISP account account in Mail.app.


← prev: Mail SSL Setup next: SendEmail →

Personal tools