2.2.11 Mail Server
2.2.1.1 Sendmail(for delivering mail)
2.2.1.2 Fetchmail(for fetching mail from remote servers)
2.2.1.3 Popper(for giving the pop service to our local hosts)
2.2.1.2 Fetchmail
Before going over the details of setting
up fetchmail we will introduce a script which uses ftp to pop mails from a remote machine.
While fetchmail is obviously superior to this little script it will give you a quick way
of popping your mails from your mail server:
----------------- ftpfetchmail.sh ----------------------
#!/bin/sh
# this script was created for maple
# to fetch mailbox 'es from a remote
# host to localhost. It was created
# created for the users lazy like us
# and who unfortunately didn't have the
# fetchmail ready at the very beginning
# #USER=junan
#PASS= # put your password here
# read the target host name
# this is the host/server where your
# emails are stored
echo -n "Target host: "
read HOST
# if nohost name is specified use the
# default address.....
if [ "$HOST" = "" ]; then
echo -n "Using default host."
HOST=202.251.34.161 # Using address rather than
# a name. Don't use nameserver
# name: mercury-gw.st.yatsushiro-nct.ac.jp
echo "$HOST"
echo "Default user name is: $USER"
echo -n "USER name at the $HOST [$USER]: "
read USER
if [ "$USER" = "" ]; then
echo -n "Using default login name: "
USER=`whoami`
echo $USER
echo -n "Your Password: "
read PASS
# no default password
if [ "$PASS" = "" ]; then
echo "No password is not permitted"
exit 1
echo "Please use full pathname......."
echo "Default file is /var/spool/mail/$USER"
echo -n "What file do you want to bring [/var/spool/mail/$USER]: "
read FILENAME
# use the default file where your mail is stored in the
# server(mercury stores files in /var/spool/mail/ directory)
if [ "$FILENAME" = "" ]; then
echo -n "Trying to bring default file: "
FILENAME=/var/spool/mail/$USER
echo "$FILENAME"
ftp -n $HOST << _EOF_
quote user $USER
quote pass $PASS
get $FILENAME tmp_mail.$$
quit
_EOF_
if [ -f tmp_mail.$$ ]; then
cat tmp_mail.$$ >> /var/mail/`whoami` # please make sure that
# local mailbox is in the
# proper location
rm -rf tmp_mail.$$
else
echo "Unable to bring the files. Sorry...."
echo "Please look at the target host, make"
echo "sure that your host(`hostname`) can "
echo "reach $HOST or there can be some "
echo "problem with the ftp server "
----------------- End of ftpfetchmail.sh ----------------------
Since we are sitting under a lot of proxies as can be seen by the
figure 2.2 it's necessary to pop up mails for our local users. Moreover there are few
users who do have different names in different servers or domains. Fetchmail is a nice
service for them to collect all the mails in a single place, hopefully in maple.
Installing fetchmail is as easy as described before. Just get the package from the
http://www.freebsd.org or ftp://ftp.freebsd.org and use the 'pkg_add' command. It will get
installed without any problem. Here is the demonstration though it's not necessary:
maple# pkg_add fetchmail-4.7.0.tgz
Give a visit to /var/db/pkg/fetchmail-4.7.0 and
read the +CONTENTS file there, it should give you an idea where all the files are
installed. Now that we installed it we can post a message in /etc/motd file telling our
users that fetchmail is available for them.(/etc/motd file is displayed whenever someone
logs in). Now let's see how to write a .fetchmailrc file for our purposes. To give a easy
way out for the first time users we made this script(actually it shouldn't be called a
script):
---------- Start of fetchrc.cre script ------------------
#!/bin/sh
# this script was created by us on maple
# for the users lazy like us. It creates the
# .fetchmailrc file in the home directory necessary
# to pop mails from remote servers(And it deletes
# the file as soon as your business is finished, we
# hate putting the plain passwords in plain files!).
# there are few defaults. You might want to set them
# rather than getting input from the standard input
# all the time(except you PASSWORD)
# please refer to fetchmail(1) for more details
MYHOME=$HOME
FILENAME='.fetchmailrc'
if [ -f $MYHOME/$FILENAME ]; then
echo "You got the file: $MYHOME/$FILENAME already!"
exit 1
else
touch $MYHOME/$FILENAME
chmod 600 $MYHOME/$FILENAME
# default host
# HOST=202.251.34.161
# use below two lines if you want to
# get server from the standard input
echo -n "Your mailserver name($HOST): "
read HOST
# use below two lines if you want to
# get username from the standard input
#echo -n "Your user name at $HOST: "
read RUSER
# if you have the same name on the remote server
#default user
#RUSER=$USER
echo -n "Your password: "
read PASS
echo "poll $HOST protocol auto" >> $MYHOME/$FILENAME
echo " username $RUSER password $PASS" >> $MYHOME/$FILENAME
echo " no mimedecode" >> $MYHOME/$FILENAME
echo " no rewrite" >> $MYHOME/$FILENAME
# keep mails intact in the remote server
fetchmail --keep
rm -rf $MYHOME/$FILENAME
---------- End of fetchrc.cre script ------------------
When you run the script with sh fetchrc.cre you
will get a temporary file .fetchmailrc in your home directory like:
------------ Start of .fetchmailrc file ----------------
poll 202.251.34.161 protocol auto
username s96281 password mypass
no mimedecode
no rewrite
------------ End of .fetchmailrc file ----------------
The 'poll' keyword at the first line tells fetchmail to query the
mailserver preceding it and to use protocol auto. Protocol auto means that it will query
the server trying with the available protocols POP2, POP3, apop, kpop, imap etc. If you
are sure about the protocol your mail server provides you can put that protocol name here
of-course fetchmail needs to support the protocol too. Second line gives the username to
be used by fetchmail to fetch programs from the remote mailserver, password following it.
As you can see you password must be in plain text and that's why fetchmail needs a
.fetchmailrc file in 0600 mode - this is the reason we hate using this file with a
permanent place in our homedirectory, it worse than having '+ +' in .rhosts! Mimecode
option converts quoted-printables to 8-bit in a MIME message, we disable it by a preceding
no. Rewrite, which rewrites the headers in the mails, is also disabled by a preceding no.
If you want to start fetchmail from the crontab make an entry in the crontab file
using(refer to crontab(5)):
maple@junan:/home/junan{1008}% crontab -e # to start editing
# min h dayofmonth month dayofweek
59 * * * * /usr/local/bin/fetchmail >/dev/null 2>&1
It will fetchmail according to the .fetchmailrc file available at your
home directory. So if you don't have a file .fetchmailrc with proper instructions in your
home directory cron won't be able to fetch mail. |