Play poker games on poker online and a complete review 888Poker review

Personal email server

Print

On this page I describe how I installed my own mail server for personal use

Warning: Use the information on this website at your own risk. It works for me, but it might not work for you..
Warning 2: You will need some proficiency with Linux and especially the Linux command line to be able to use the information on this website.

Why have your own mail server?

I wanted to have my own mail server, for a number of reasons:

  • I wanted to integrate email from several online accounts in one place
  • I wanted to reduce spam , using my own spam filter on the incoming email
  • I wanted to have my email archive independent of the ISP and/or the email client that I am using
  • I was looking for a solution to have push email for my phone
  • An integrated email /calendar solution that enables me to access my email in a number of different ways( I found that this is possible using Zarafa)

Design

I decided to build my mail server on a small Intel Atom powered PC. Linux was the obvious OS for me, because it is open source and there is a lot of software available to enable such a setup. Furthermore I was already familiar with Linux.

I chose Ubuntu as the Linux distribution for this server, as I am already familiar with Ubuntu setup and tools and I like it.

Based on previous experience, I picked the following software packages to build my email server:

  • Getmail: Download email from several POP3 accounts to my mail server.
  • ClamAV: one of the most common open source virus scanners. Every email that comes in will be checked for virusses before being delivered to the inbox .
  • SpamAssassin: A commonly used spam filter.
  • Maildrop: This piece does the sorting ,filtering and delivery of the emails.
  • Zarafa: The email platform that allows me to access my email and calendar through IMAP and POP and even MAPI(Outlook).
  • It can also be used to provide push email to your mobile phone and it has webmail integrated.
  • Postfix: One of the most common open source email servers. I actually only use the SMTP functionality for the outgoing email.

The email flow is as follows:

The incoming email is retrieved by getmail. I prefer getmail over fetchmail , because it allows me to not directly delete the emails that I retrieve from the mail server of my ISP. This way I always have the email on my ISP's mail server for some time, just as backup. For the rest I didn't find any major differences.

Getmail forwards the emails directly to maildrop. Maildrop then filters the emails using clamAV and SpamAssassin to filter out and virusses and/or SPAM. It can also do some additional filtering and sorting based on the subject and sender. It then forwards the email to Zarafa. Zarafa then stores the email in a MySQL database. I can then retrieve my email from a PC or smartphone.

To send emails , I use Postfix . I could actually configure the mail clients to send email directly to my ISP SMTP server, but I wanted the server also be able to send emails itself, in case of problems or errors that occur. Probably Sendmail would do the job as well, by the way.

 

Installation

I started by downloading the server edition of Ubuntu  and using that to install Ubuntu on the harddisk. The questions during installation are rather straightforward. When chosing the software packages, I installed:

  • OpenSHH server (so I can remotely control this server)
  • LAMP server (this is useful because we need a MySQL server for Zarafa and Apache is used for the webaccess to the email)

After the installation is done, you need to restart and you end up with a login prompt. Login with the user your configured during the installation and then we're ready to go

Additional software

Let's first log in as root (super user) to further install and configure the server using the following command.

 
sudo -i
 

Now let's first bring our server up-to-date with this command

 
aptitude safe-upgrade
 

Let's install the tools to do get our email , do virus scanning and spam filtering and deliver them.

 
apt-get install getmail4 maildrop postfix clamav clamassassin
 
#install spamassassin and some other packages for added functionality to spamassassin.
apt-get install spamassassin  libnet-dns-perl libmail-spf-query-perl pyzor razor  
 

Probably it will ask you to install some other additional packages as well... Well, now we need to configure all these tools and then we can install Zarafa.

Maildrop

Maildrop will actually filter your email through the spam filter and the virus scanner and it can also sort the email for you, if you'd like. I could have used something like Amavis to do the email filtering, but I used Maildrop also previously for my email sorting and I know how it works. It is not really fast, but it's fast enought to cope with only my personal email.I have used Amavis and similar tools to do virus and spam checking, but I feel that it is a lot of overhead for a mail server with just a few users.

A common way to configure maildrop is by creating a .mailfilter file in the home directory of your Linux user. For example /home/jeroen/. The .mailfilter file could look like this:

 
 
#let's first pass our email through the virus scan
xfilter "/usr/bin/clamassassin"

#and now through the spam filter
xfilter "/usr/bin/spamassassin"
 
if(/^X-Virus-Status: Yes$/)
{
 # if it's a virus, remove the email
  to "/dev/null"

}
 
if(/^X-Spam-Flag: YES$/)
{
#send spam email to a special folder in my inbox.
  to "|/usr/bin/zarafa-dagent jeroen -F spam -s"
# in the latest versions of Zarafa this is not necessary anymore, as the zarafa dagent itself will also look 
# for the X-Spam-Flag header and put spam in the "Junk Mail Folder"
}   #if it's a normal email, send it to my Zarafa inbox to "|/usr/bin/zarafa-dagent jeroen -s"  

If you want to know more about maildrop, just use the maildrop manpages:

 
man maildrop
 

or have a look at the website: http://www.courier-mta.org/maildrop/

SpamAssassin

Even though SpamAssassin was already installed earlier on, it is not yet running. Under Ubuntu it is not enabled by default. Go to /etc/default/spamassin and set

 
ENABLED=1
 

Furthermore I prefer the spam email not to be attached as an MIME attachment to a report email. I just want a spam header on the original message, such that Maildrop can then sort out what to do with the spam message.To enable this behaviour, you can go to /etc/spamassassin/local.cf and set

 
report_safe 0
 

In this file you can also change many other settings of SpamAssassin, but I just left it as it was. As you might have noticed, the Bayesian classifier in SpamAssassin is by default enabled. But if you want the Bayesian classifier to be of any use, you will need to train it. I will get back to this later on.

Getmail

If you have maildrop setup, it's time to setup getmail to actually get your email. I execute getmail as my standard Linux user, not as root. To tell getmail where it should download the email, you'll have to create a folder called ".getmail" in your Linux user home directory. In this folder you create a file for each account that you have, named like getmailrc0, getmailrc1, getmailrc2, getmailrc3, etc. Use exactly this naming convention, otherwise getmail will not be able to find your account details. I will give an example for getmailrc0:

 
[retriever]
type = SimplePOP3Retriever
server=pop.youremailserver.com
username=yourusername
password=yourpassword
 
[destination]
type = MDA_external
path= /usr/bin/maildrop
user=jeroen
group=jeroen
 
[options]
#I want to keep the email on the POP server for 21 days before removing it

delete_after=21
# don't download all the email every time, only download the email I haven't downloaded yet.
#This is important if you don't delete your email from the POP server immediately
read_all=false
#Don't add a new Received header to the email, but use the existing Received header
received=false
 

You can find more information about getmail at : http://pyropus.ca/software/getmail/

Installing Zarafa

Start by downloading the latest version of Zarafa from their website at http://www.zarafa.com/download-zarafa. The Zarafa software is only released for the LTS versions of Ubuntu, so it's good to take this into consideration for your Ubuntu installation. If you would like to use Outlook and the need the MAPI support, download the version with 3 users Outlook support. Unpack the zarafa installation files using something like:

 
tar -xzf zarafa-6.40.0-ubuntu10.04-i386.tar.gz

 

In the folder where you just unpacked all the files is a install.sh file. Run it like:

 
./install.sh

and it will guide you through the entire installation process of zarafa. It will also setup the MySQL database for Zarafa and install dependencies if there are any.

You can still change settings to the Zarafa server in /etc/zarafa/server.cfg once you completed the installation. Because I prefer the attachments to be stored outside the MySQL database, I set :

 
attachment_storage = files

. Don't forget to restart the server after you made any changes.

The first test

Webaccess for zarafa should be up and running now. You can test it by going to http://yourservername/webaccess/ and this should bring up the webaccess screen for Zarafa. You can log in and see if it works.But ofcourse your email inbox is still empty... Let's do something about that.

Getmail get's your email

I made the following small script that can be executed by crontab. It will actually download your email and it should end up in your Zarafa inbox. getmail.sh :

 
setlock -n /tmp/getmaillock getmail --getmaildir=/home/jeroen/.getmail --rcfile getmailrc0 
 

I used setlock to make sure there can only be one instance of the script active. Otherwise when the script is already running and the crontab already starts it again, a second instance of getmail is running and that might give errors and strange behaviour. Setlock is not standard installed, install "daemontools" with apt-get to get it installed. Also make sure the /tmp folder is writable for the user executing this script.

Execute the getmail.sh script with your regular Linux user , not with root. If everything goes well, you should see getmail downloading your emails. Once the script is finished, you can log in to the Zarafa webaccess again to see whether it has succesfully delivered the emails to your Zarafa inbox.

Using Outlook to access your email

If you would like to use Outlook to connect to your Zarafa server, you first need to install the Zarafa Client (which is a kind of a plugin for Outlook. This can be downloaded from http://www.zarafa.com/download-zarafa )

Zarafa comes also with good documentation, for both the server administrator as well as a user manual. These can be found at http://www.zarafa.com/content/documentation

Zarafa backup script

I wanted a simple script to backup all my email to a NAS (Network Attached Storage) on my network. This NAS is already mounted as a NFS share on my Linux server. I use the following script to make a complete backup of all my email on the Zarafa server. First I make a dump from the Zarafa database on the MySQL server and then I make a backup of all the attachments seperately.

 
echo "Zarafa email backup"
 
# Setup variables for the archive filename.
day=$(date +%A)

daynumber=$(date +%u)
name="$daynumber-$day"
echo "name = $name"
 
if [[ ! -e /mnt/backup/Email/zarafa/$name/  ]]

then
mkdir /mnt/backup/Email/zarafa/$name/
fi
 
echo "Creating zarafa MySQL dump"
mysqldump --single-transaction -uzarafa  -pmyownpassword zarafa > /mnt/backup/Email/zarafa/$name/zarafa.sql
#now create a tar.gz file of all attachments

echo "Creating zarafa attachments tar"
tar -czf /mnt/backup/Email/zarafa/$name/attachments.tar.gz -C /var/lib/ zarafa
 

I run this script on a regular basis using cron.Warning: Executing this script might take a long time to complete if you have a lot of email and attachments.I run it at night, when not much else is going on on the server anyway.In this time period I don't run the getmail script, because if new mail comes in while making the backup, the MySQL dump and the attachments file might be out of sync.

2011 Personal email server - Jevontech.
Joomla