"Linux Gazette...making Linux just a little more fun!"

Setting Up Mail for a Home Network Using Exim

By Jan Stumpel

[Revised at author's request. Originally published in issue #42.]

1 Introduction

Setting up a home network with Linux and Win95, using Samba, IP Masquerading, and diald has been described many times, also in the Linux Gazette, but so far I have not found a recipe for setting up mail on a small network with only one dial-up e-mail account. In this article I want to explain how I did it. With this system:

This is realized on my system (running Debian Linux 2.1) using the following programs:

I have this set up for two machines (1 Linux + 1 Win95) but it will probably also work for a somewhat larger network, and may be sufficient for a small office. Note: this article is Debian-oriented. If you use another distribution, change where appropriate!

2 The network and the names

For this article I assume the following names (change these to correspond with your own situation):

I also assume that the local networking works, and that there is on-demand dialup access using diald. There is no name server on heaven. /etc/resolv.conf contains the addresses of two name servers supplied by the ISP. These same addresses are entered into the TCP/IP configuration on earth.

/etc/hostname on heaven is


/etc/hosts on heaven is localhost heaven.home heaven earth.home earth

On earth there is a file c:\windows\hosts with the same contents as /etc/hosts.

3 Mail addresses

Mail messages can have more than just the address in the 'To:' and 'From:' lines, for instance :

To: Emily Bloggs <joe.bloggs@isp.com>

'Emily Bloggs' in the above example is the 'real-name part'. It is set in the e-mail program which composes the message. This 'real-name part' can be used for delivering Emily's mail to her. Note: if the 'real-name part' has dots in it, it must be quoted using " characters ("Joe C. Bloggs"). See also man mailaddr.

4 Configuring exim

On a Debian system this is done by running eximconfig. It asks a number of questions which you can answer as follows:

In MS Internet Mail (or whatever mail client you use on Win95) heaven must be entered both as the SMTP server and as the POP3 server. Under 'pop3 account' and 'pop3 password', enter the username emi and her Linux password. Enter the the name, Emily Bloggs, and the e-mail address, emi@home, in the appropriate place. Note that the e-mail address must be in the local domain!

On the Linux side, nothing special has to be set. /etc/pine/conf and the users' ~/.pinerc can be used 'out of the box'. The mail client (pine) constructs local addresses using the hostname together with user information from /etc/passwd.

With the above setup, local users can happily send mail to each other and reply to it. For instance, in pine at heaven, user joe sends mail to user emi. Automatically, pine changes this to:

To: Emily Bloggs <emi@heaven.home>

The message is delivered immediately (as you can see if you run eximon, the exim monitoring utility). emi (should she log in to heaven) would see the message as coming from

From: Joe Bloggs <joe@home>

So home really functions like a local domain within which messages can be exchanged. The problem is sending messages to the outside world. A From: address like <joe@home> is no good because nobody on the outside could reply to an address in the non-existent domain home.

5 Fixing the From: address

We must change the local From: address into a valid e-mail address (the e-mail account at the ISP), but only in the case of outgoing messages. With exim, we can do this by means of a 'transport filter'. The outgoing mail passes through this filter, and the From: address is changed. Local mail will not be affected.

The following filter will do the trick, provided we are sure that the address that we want to change is always between < and > signs. This is not guaranteed, but very common: pine, mutt, and mail, as well as MS Internet Mail all generate such addresses.

$address = 'joe.bloggs@isp.com';
while (<>) {
    if (/^From: /) { s/<.*>/<$address>/; print; last; }
    print; }
while (<>) { print; }
Don't forget to change the e-mail address to yours! Call this program outfilt, do chmod +x outfilt and put it in /usr/local/bin. Now we must add a line to /etc/exim.conf, so the last lines of the TRANSPORTS CONFIGURATION section read:

   driver = smtp
   headers_remove = "sender"
   transport_filter = "/usr/local/bin/outfilt"
Actually, we added two lines. The headers_remove line is also new. This prevents exim from adding a Sender: header to the message (as it would do with this setup, if you use pine). The Sender: line can cause trouble with some (badly configured) mail destinations.

With these changes to /etc/exim.conf, whenever anyone sends an e-mail message to the outside world it is now delivered properly by exim. Exim (through diald) opens the outside line at once. In a home situation this is probably what you want. In a small office, with a lot of e-mail traffic, you may want to defer messages and send them as a bunch at certain times, to save phone costs. This is possible, but I don't need it myself and have not looked into it. You could look at the 'Linux Mail-Queue mini-HOWTO'.

6 Fetchmail configuration

At the command fetchmail diald opens the line and the mail from the ISP is collected (and passed to exim for local delivery). Only users who have a .fetchmailrc, owned by themselves, in their home directory can run fetchmail. This file can be created using the configuration tool fetchmailconf. You get something like:

# Configuration created Sun Mar 28 03:15:20 1999 by fetchmailconf
set postmaster "postmaster"
poll pop3.isp.com with proto POP3
       user "jbloggs" there with password "zaphod" is joe here options fetchall warnings 3600
The .fetchmailrc files belonging to the various users could all be copies of each other, but with the ownership set to the user concerned. It is not so nice that every user has the password in plain view. Maybe there is a better way, but in a home situation it does not matter.

The main point is that whoever runs fetchmail, the mail must always be delivered to the same user mailbox (joe's mailbox in this case).

7 Removing exim's delivery limit

Exim by default does not deliver more than 10 messages at a time. I am sure there are circumstances where this makes perfect sense, but having a dialup account is not one of them. To get rid of this restriction, you must put into the MAIN CONFIGURATION section of /etc/exim.conf, before the end statement, a line

smtp_accept_queue_per_connection = 0

8 Delivering personal mail

Through fetchmail and exim, all mail from the outside is by default delivered to Joe's mailbox (var/spool/mail/joe) at heaven. In Joe's home directory he puts a file called .forward, containing the following text:

# Exim filter
if $header_to: contains Emily then deliver emi endif

The .forward file must have permissions -rw-r--r--. If you're not sure, give the command chmod 644 .forward.

If mail contains 'Emily' in (the 'real name part' of) the To: address (and this will almost always be the case when her friends send her mail) it will now go into her mail account on heaven, not into Joe's. She can move the mail to her own machine using POP3 (see below).

Delivery to other users than Emily can be arranged with elif ... then clauses in the .forward file. Actually, exim's .forward files can perform a lot of complicated functions. See the text "Exim's user interface to mail filtering" which comes with the exim doc's.

9 Transferring mail with qpopper

To let heaven act as POP3 server for earth, qpopper can be installed. I installed the Debian package qpopper_2.3-4.deb. Installation is automatic; no configuration is necessary. If Emily presses 'get/send messages' in MS Internet Mail, the contents of her mailbox on heaven get transferred to earth (and all mail, local or outside, which she has written gets delivered).

10 Automatic mail notification

Emily likes to be notified if mail arrives for her at heaven. If Samba is installed on heaven and Winpopup on earth, this is easy. Mail notification on earth can be done using smbclient (a program which comes with Samba).

Joe (as root) has put a Perl program called mailwarn into usr/local/bin:

open  POPUP, "| smbclient -M $ARGV[0] >/dev/null 2>&1";
print POPUP "It's from $ARGV[1]";
close POPUP; 

This file was, of course, first made executable using chmod +x mailwarn.

In Emily's home directory at heaven there is now also also a .forward file (emi is the owner; permissions are 644):

# Exim filter
unseen pipe "/usr/local/bin/mailwarn earth $header_from:"

If mail (local or from the outside) for Emily arrives at heaven, a window will now pop up on earth telling her this.

11 Manually collecting the outside mail

Thanks to a 'shortcut' on earth's Win95 'desktop', which does a telnet to heaven, Emily can log into heaven and start fetchmail by hand. That is, if she does not want to wait for the scheduled cron times when fetchmail runs. After the mail has been transferred from the ISP, she can press 'get/send messages' to move any mail from her heaven mailbox into the earth one.

Copyright © 1999, Jan Stumpel
Published in Issue 43 of Linux Gazette, July 1999