15.3. E-Mail

15.3.1. Introduction

A short introduction about how to setup email on a laptop used at home (dial-up) and work (ethernet) by Peter Englmaier <ppe_AT_pa.uky.edu>:

15.3.1.1. Features

As a laptop user, I have special demands for my email setup. The setup described below, enables me to:

  • Read my email from home using a POP email server, which is supplied by my university, but could also be setup on a work place computer.

  • Write email from home with the right return address in the email (which does not mention my computer name).

  • Read/write my email while working on a workstation without access to my laptop or the POP email server (as a backup).

  • Read my email while working on my laptop connected to the ethernet of our institut.

  • Direct email while connected via ethernet (faster than the fetchmail method).

  • Indirect email (over pop mail server) while not connected to the ethernet at work (either at home via modem or somewhere else via ethernet).

  • Use any emailer, e.g. elm or the simple mail command.

  • Sort incoming email, delete spam, split email-collections (digests) into seperate emails

The configuration is based on sendmail, fetchmail, and a remote pop account for email.

15.3.1.2. Configuration of sendmail

This is the most complicated part. Having installed the sendmail-cf package, I created a file named /usr/lib/sendmail-cf/laptop.mc:


divert(-1)
include(`../m4/cf.m4')
define(`confDEF_USER_ID',''8:12'')
define(`confBIND_OPTS',`-DNSRCH -DEFNAMES')

# here you define your domain
define(`confDOMAIN_NAME',''pa.uky.edu'')
OSTYPE(`linux')
undefine(`UUCP_RELAY')
undefine(`BITNET_RELAY')

# there we send outgoing email
define(`SMART_HOST',`server1.pa.uky.edu') 

# there we send mail to users my laptop does not know
define(`LUSER_RELAY',`server1.pa.uky.edu') 

# again the domain, we want to be seen as
MASQUERADE_AS(pa.uky.edu)
FEATURE(allmasquerade)
FEATURE(nouucp)
FEATURE(nodns)
FEATURE(nocanonify)
FEATURE(redirect)
FEATURE(always_add_domain)
FEATURE(use_cw_file)
FEATURE(local_procmail)
MAILER(procmail)
MAILER(smtp)
HACK(check_mail3,`hash -a@JUNK /etc/mail/deny')
HACK(use_ip,`/etc/mail/ip_allow')
HACK(use_names,`/etc/mail/name_allow')
HACK(use_relayto,`/etc/mail/relay_allow')
HACK(check_rcpt4)
HACK(check_relay3)

This looks more complicated as it is. All it does is, that it redirectes outbound mail to server1 (SMART_HOST) and also mail for local users which are not known (LUSER_RELAY). That way, I can write email to my colleques without using their full email address. More important: the From line in my email points back to my MASQUARADE_AS domain and not directly to my laptop. If this where not the case, email returned with the reply button might not reach me. You must restart sendmail for changes to take effect. Note: this configuration is for Redhat 5.2 systems. You may have to change some details.

Now, all what is needed is to generate the /etc/sendmail.cf file m4 laptop.mc >/etc/sendmail.cf and to add all possible domain names my laptop should respond to in /etc/sendmail.cw:


# sendmail.cw - include all aliases for your machine here.
laptop
laptop.pa.uky.edu
128.17.18.30
guest1
guest1.somewhere.org

It is important to have all aliases in this file, otherwise sendmail will not accept the mail (and will reply we don't relay to the sender). Finally, you must now test the setup by sending email, replying to mail for all possible configurations. Any missconfiguration can result in loss of email.

15.3.1.3. Configuration for fetchmail on Laptop

One method to get the email into your machine is through fetchmail. Fetchmail periodically checks for new email at one or more remote mail servers. I use the following fetchmail configuration file (in my user home directory): fetchmailrc


set postmaster "myusername"
set daemon 900
poll pop.uky.edu with proto POP3
user "mypopusername" there with password "mypoppassword" is mylaptopusername here

Fetchmail will just get the email and send it to sendmail which will it deliver into your /var/spool/mail/$USER file.

15.3.1.4. Forward E-Mail to the Laptop

On my work station I have the following .forward file:


me@pop.acount.edu,me@server1

Here server1 is the machine where I keep my mailbox. All email is send to the pop account to be picked up later by my laptop (using fetchmail). However, when my laptop is connected via ethernet, I want my email to go directly to the laptop, instead of pop:


me@laptop,me@server1

In both cases, a backup of my email is send to server1 (where I also can read it, in case I cannot get my laptop). I keep/store all email on the laptop.

Switching is done by three script files and a crontab file (on the workstation):

forward_pop


#!/bin/sh
echo "me@pop.acount.edu,me@server1" > ${HOME}/.forward

forward_laptop


#!/bin/sh
echo "ppe@laptop,ppe@server1" > ${HOME}/.forward
crontab ${HOME}/mycrontab
${HOME}/utl/check_laptop

check_laptop


#!/bin/sh
if /usr/sbin/ping -c 1 laptop  >/dev/null 2>&1 ; then
   :
else
   # redirect mail to pop
   ${HOME}/utl/forward_pop
   sleep 10
if /usr/sbin/ping -c 1 laptop  >/dev/null 2>&1 ; then
      # back to normal
      ${HOME}/utl/forward_laptop
else
# deactivate crontab check
/bin/crontab -l | grep -v check_laptop >${HOME}/tmp/mycrontab.tmp
      /bin/crontab ${HOME}/tmp/mycrontab.tmp
      rm -f ${HOME}/tmp/mycrontab.tmp
fi
fi

mycrontab


# mycrontab
0,10,20,30,40,50 * * * * ${HOME}/utl/check_laptop

Each time I connect the laptop to the ethernet, I have to run forward_laptop, and each time I disconnect I run forward_pop. In case I forget to run forward_pop, the crontab job runs it for me less then 10 minutes later. To do all that automatically, I change the network script files on my laptop as follows:

/sbin/ifdown (this script runs, whenever a network device is stopped, new stuff between BEGIN and END)


...
fi
# BEGIN new stuff
# turn off forwarding email
mail ppe <<EOF
turning off forwarding email
device = ${DEVICE}
hostname = `hostname`
EOF
if [ "${DEVICE}" = "eth0" -a "`hostname`"
= "laptop" ]; then
su -lc "ssh -l myusername server1
utl/forward_pop" myusername >& /dev/null
fi
# END new stuff

ifconfig ${DEVICE} down
exec /etc/sysconfig/network-scripts/ifdown-post $CONFIG

Note, that the script checks for the value of hostname. In case, I am connected to a foreign ethernet, my hostname and ip-address will be something else, e.g. guest1.

/etc/sysconfig/network-scripts/ifup-post (this script is run, whenever a network device is started)


# Notify programs that have requested notification
do_netreport
# BEGIN new stuff
# check for email -- I'm using fetchmail for this
if [ "${DEVICE}" = "eth0" -o "${DEVICE}"
= "ppp0" ]; then
su -lc fetchmail myusername >& /dev/null &
fi
# set clock if connected to ethernet, redirect email
if [ "${DEVICE}" = "eth0" -a "`hostname`" = "zaphod" ]; then
( rdate -s server1 ; hwclock --systohc --utc ) >& /dev/null &
# forward email
su -lc "ssh -l myusername gradj utl/forward_laptop" myusername >& /dev/null &
fi
# END new stuff

exit 0

15.3.1.5. Processing Incoming E-Mail with procmail

This step is completely optional. The above described sendmail configuration calls procmail for each received email, but you could have called procmail using the .forward file (see the procmail man page). Procmail is a handy tool to block spam and to sort incoming email.

You need to setup a .procmailrc file to use procmail. See the man page for procmail, procmailrc, and procmailex (examples). My setup demonstrates, how to ignore certain email messages and split email-collections (digest) into pieces:


# -- mail filtering -- procmail is called by sendmail --
PATH=/bin:/usr/bin
MAILDIR=$HOME/Mail
LOGFILE=$MAILDIR/from
# keep in mind:
# use ":0:" when writing to a file
# use ":0"  when writing to a device, e.g. /dev/null, or send email

# - make a backup of *all* incoming mail, but ignore mail tagged below -
:0 c:
*! ^Sissa-Repro
backup

# - keep only last 50 messages
:0 ic
| cd backup && rm -f dummy `ls -t msg.* | sed -e 1,50d`

# - delete email coming through the 'postdocs' email list, when
# it is not of any interest
:0
* ^From.*postdocs
* ^From.*Ernst Richter /dev/null :0
* ^From.*postdocs
* ^Subject.*card charge
/dev/null
# Split mailing list from the sissa preprint server into individual emails
# - this is quite complicated :(   I can flip through the list much
#   faster and ignore preprints which have uninteresting titles. Instead of
#   having to browse through the whole list, my mailer will just present a
#   list of papers.
# 1. split it in individual messages
:0
* ^From no-reply@xxx.lanl.gov
| formail +1 -de -A "Sissa-Repro: true" -s procmail
# 2. reformat messages a bit
# 2.1. extract 'Title:' from email-Body and add to email-header
as 'Subject:'
:0 b
* ^Sissa-Repro
*! ^Subject
TITLE=| formail -xTitle:
:0 a
|formail -A "Subject: $TITLE " -s procmail

# 2.2. store in my incoming sissa-email folder. Here, we could
#      also reject (and thereafter delete) uninteresting 'Subjects'
#      we could also mark more interesting subjects as urgend or send a copy
#      to regular mail box.
:0:
* ^Sissa-Repro
* ^Subject
*! ^replaced with
sissa

By the way, there is a tk GUI tool to configure procmail (I think it is called dotfiles).

15.3.2. Email with UUCP

Another possible solution for Email is to use UUCP. This software was made for disconnected machines, and is by far the easiest solution if you have several users on your laptop (we are talking about UNIX, remember?), each with his/her own account.

Unlike what most people think, UUCP does not need a serial connection: it works fine over TCP/IP, so your UUCP partner can be any machine on the Internet, if it is reachable from your network attachment point. Here is the UUCP sys for a typical laptop:

system mylaptop
time any
chat "" \d\d\r\c ogin: \d\L word: \P
address uucp.mypartner.org
port TCP

15.3.3. MailSync

Mailsync is a way of synchronizing a collection of mailboxes. The algorithm is a 3-way diff. Two mailboxes are simultaneously compared to a record of the state of both mailboxes at last sync. New messages and message deletions are propagated between the two mailboxes. Mailsync can synchronize local mailbox files in many formats and remote mailboxes over IMAP, POP, and IMAPS.