Raspberry Pi Email Server

Powered by Drupal

raspberry-pi-email-server.png

The RasPi’s small size and low power consumption make it an ideal choice for use as a home email server. After trying a couple of different pieces of software, I finally found an excellent combination: Postfix with Dovecot and Squirrelmail, plus Spamasssassin and Sieve for spam filtering. There are many, many tutorials out there for the first trilogy of programs, but since the configuration is slightly different for each distribution I kept coming unstuck when setting mine up on the Pi. Having finally got mine configured properly, I’ve put together a set of 5 tutorials, which will take you from a vanilla Raspbian image to a fully functioning email server in no time. When writing the tutorial I made an effort to explain what each setting does instead of just dumping commands. With a bit of luck at the end of the process you’ll not only have a working server, you’ll understand how it works… without having to wade through reams of documentation like I did! If you follow the tutorials from start to finish, here’s what you’ll end up with:

  1. An email server that you can run 24/7/365 for under £5 of electricity per year
  2. Personalised email address like you@yourdomain.com (requires you to have registered a domain name with a registrar like namecheap.com - see my DNS basics tutorial)
  3. The ability to connect from anywhere, and read & send email, using a secure IMAP connection on your phone, tablet or computer
  4. Log in to webmail using any web browser on a secure HTTPS connection, read & send email
  5. Complete control over your personal communication. Your emails are stored on YOUR server, and nobody is scanning them to sell you adverts.
  6. Smart spam filtering with Spamassassin
  7. Customisable mail sorting with Sieve rules

Postfix, the Mail Transfer Agent

Postfix Logo

Postfix is the program that lets you send and receive email using Simple Mail Transfer Protocol (SMTP). Whilst you, the user, may connect to your email server using IMAP (on port 143 or 993), or POP (on port 110 or 995), email servers talk to each other using SMTP on port 25. So, this is the basic core of the server. Without it, you wouldn’t be able to send or receive any emails! I’ve covered the setup here: Raspberry Pi Email Server Part 1: Postfix

Dovecot, the POP/IMAP Server

dovecotLogo-300x130_0.png

Dovecot is used for two things:

  1. It provides you with IMAP functionality
  2. It checks that you are who you say you are using Simple Authentication and Security Layer (SASL) before you send or fetch mail

If you’re not interested in connecting with IMAP on your devices, you still need Dovecot. Not only is it doing SASL for you, but Squirrelmail connects using IMAP in order to provide you with webmail. I’ve covered Dovecot installation and configuration here: Raspberry Pi Email Server Part 2: Dovecot

Squirrelmail, for Webmail

Squirrelmail Logo

Squirrelmail is handy because it allows you to check your email in any browser, from anywhere. Of the first three, it’s probably the easiest to configure. I’ve covered it here: Raspberry Pi Email Server Part 3: Squirrelmail

Spamassassin, for Marking Spam

Spamassassin Logo

Spamassassin is the program that we will use to audit incoming mail and decide whether or not it’s spam. Spamassassin doesn’t actually sort the mail into the spam folder, it only changes information in the headers based on the results of the scan. I’ve covered it here: Raspberry Pi Email Server Part 4: Spam Detection with Spamassassin.

LMTP & Sieve for Spam Sorting & Mailbox Organisation

After Spamassassin has checked incoming mail to see if it’s spam or not, we need another program to sort it into the right mail folder. This final step will be done with Dovecot’s Local Mail Transfer Protocol (LMTP) daemon and a Sieve plugin. Sieve is a simple programming language that allows users to define what to do with incoming email based on a predefined set of rules – think “if the header contains this flag, put it in the spam folder” kind of thing and you’ll get the gist. Aside from spam filtering, Sieve can be used to automatically sort & de-clutter your inbox. These steps are covered in the final tutorial: Raspberry Pi Email Server Part 5: Spam Sorting with LMTP & Sieve Enjoy! I’d love to hear how you get on, so leave a comment below :)

Comments

I'm able to successfully send and receive email using Thunderbird to and from my pi email server account. Squirrelmail is running on my separate ownCloud pi.

When I log into squirrelmail (using Firefox or Chromium on Linux), I can check my mail, change options, etc. When I compose a message and try to send, I see "Waiting for mydomain.com" for a while then I get:
ERROR:
Message not sent. Server replied:

While ssh'ed into the pi and doing tail /var/log/mail.log I see:
Jan 31 15:49:26 mydomain postfix/smtps/smtpd[2785]: SSL_accept error from MY-WAN-IP.lightspeed.mytown.sbcglobal.net[MY.WAN.IP]: -1
Jan 31 15:49:26 mydomain postfix/smtps/smtpd[2785]: warning: TLS library problem: 2785:error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol:s23_srvr.c:647:
Jan 31 15:49:26 mydomain postfix/smtps/smtpd[2785]: lost connection after CONNECT from MY-WAN-IP.lightspeed.mytown.sbcglobal.net[MY.WAN.IP]

Any ideas?

Thanks for the POP3 link Sam, (i thought i had skimmed through all of the comments, must have missed that one.
The authentication problem i get with squirrelmail is :- if i type the ip address of my pi 192.168.1.104 into my browser i can enter one of the usernames and pass and login with no problem. but if i type my domain jordon.org into the address bar i get the squirrelmail login screen but if i enter any of the usernames i get the error "Access denied. Please contact your system administrator"

Are you using the full email address as your username? I don't use squirrelmail any more but I've had funny behaviour from it in the past where it accepts my full email address but not just "sam". I visited your site just now and noticed you're not rewriting HTTP to HTTPS - make sure you do that, you don't want to send your PW in the clear. Sam

I've just set up a little email server with the assistance of your tutorials on a Raspberry Pi2. My ISP only offers dynamic addresses so, as you suggested I registered a new domain with the unfortunately named namecheap registrar. I've had a domain on UK2 for about 10 years, but they don't offer DDNS. All is working well, thanks very much for your efforts!!

Thank you very much for this article ! I have been the whole day working on this and finally had success.
But I think the Web UI of Squirrelmail is not that beautiful. And I find that RoundCube is so nice that I really like it.
So I have searched a little while for the solution of Postfix+Dovecot+Roundcube. For example https://wiki.archlinux.org/index.php/Virtual_user_mail_system#Roundcube

To be honest, I haven't totally understood what I have done when following your tutorials. So when I try to read another Tutorial to setup Roundcube, I don't know which settings I should keep or change in so many configure files. It's really confusing.
So, could you spend a little more time writing something about Roundcube? That would be very nice !
Thank you again for your help !

Hi David, Which model of pi do you have? The pi 1 isn't fast enough to run RoundCube, but the pi 2 is... the tutorial was written before the pi 2 came out, and there are still lots of people with pi 1s, so I stuck with the one that would always work. I agree about RoundCube being nicer (I use it on my server, which I upgraded from a pi to an Intel NUC). I don't have time to write a RoundCube tutorial now but might do it in the future. Thanks for commenting! Sam

Thanks for your reply!
I have bought the Pi 2 for about a month and then I have tried several projects like OpenVPN, WordPress, Pi-HiFi and so on.
Now I want to build my own Email-System, but it seems to be more difficult than other projects and it contains 3 parts.
But I will not give up : ) .
Hope to see your further Tutorials !
I wish I could find the solution before your tutorials coming out : )

Hi,

Great tutorial, set up my mailbox to test it out and for the most part it works a charm,

however I'm getting duplicate mails in my inbox

If I mark the mail as unread (say by reading it) it creates a second copy in my inbox which is also unread, I've been digging but can't find anything other than its perhaps something to do with dovecot. any ideas?

Well i haven't been asked that one before! lol. Might be useful to rule out dodgy email client behaviour - which client are you using? If you connect with a different client/squirrelmail do you see the duplicate emails? Anything interesting in your mail log (/var/log/mail.log)? Sam

Hi, I think I got to the bottom of it.

It seems to work fine on other accounts which seems to indicate a problem with my client.

Found out that outlook had an AVG plugin on mine but not the others and when it scans the mail it rewrites it. The server treats it like a new mail. Disabled AVG and voila!

That's interesting, thanks for posting your fix. Next on the todo list: switch to Linux and you can get rid of that pesky antivirus altogether ;)

Karl Mowbray

Tue, 03/24/2015 - 11:07

I followed the tutorials and my pi seems to be working fine.

However... I want to use getmail to pick up my mails and drop them into the delivery queue for filtering and sorting.
I thought I needed to throw the mails at

[destination]
type = MDA_external
path = /usr/lib/dovecot/deliver

but this seems to just put all the mail into the logged in users mail folder.

Where should I inject the mails so that they are picked up and handled by your setup?

Sorry if this is vague, I have a basic understanding of whats going on but obviously not enough to completely understand the flow of incoming mails.

I will fully appreciate it if you cannot answer my query.

Hi Karl, Sorry for the late reply! I've never used getmail so I'm afraid I'm not going to be much use. If you find the answer please comment back though - I'd be interested to learn and it might help someone else! Sam

Hi Sam,

Thanks for your great series!
I have one problem, that is that my sent mail keeps getting into people's Spam boxes. For one reason or another, my mail keeps being filtered out, when it is sent from my Pi SMTP server.
Any ideas?

Kind regards

Like a good admin I have been keeping my Pi e-mail server upto date with patches.

However I ran into a problem recently which appears to have reoccurred today. Initially I thought that I had messed up, but now I am not convinced.

On the first occasion, after several weeks without problems following tutorial completion, I did an update and upgrade but on reboot my pi refused to boot.

On replacing the missing boot files, the pi booted but the mail server refused to accept my passwords, checked via SSH. It appeared that most of the files had become read only. Being a fairly new to playing with Linux I rebuilt the mail server on another SD card on the belief I had done too much at one time with the update and upgrade.

Having done an apt-get update my rebuilt e-mail server today the pi won't authenticate my password. I have logged in via SSH, so I know my password is correct. Squirrel mail is also declining to authenticate as well. Luckily this time I hopefully backed it up last night with Win32DiskImager.

Any thoughts Sam?

Hi Tim, That's bizarre, I've never come across it before. The failure to boot makes me think it's a problem with the OS and not an email server specific thing. Which boot files did you replace? Is it possible your config file tells the server to mount a partition read only or something like that? Do you have the boot partition and the root partition on the same SD card or have you moved anything to a USB stick? Sam

Sam

I replaced the boot files with those from a Raspbian distro I downloaded from the Pi site. I was actually preparing my Pi B+ at the time.

I had not knowing played with any config files other than those in your tutorials. I suspect I may have just been over zealous with my upgrades and updates. Failing to reboot between them may have caused some incompatibility, well that's my theory.

Currently all of the partitions are on the same Sandsik 32GB SD card.

I found a little script which backups up the distro using tar, so I am doing a backup on a regular basis now and the coping them off elsewhere. Hmm the Pi Cloud project might come in useful. I will most probably get a USB stick/drive to act as store for my backups.

I am plan to set up a cron job to backup /home on a nightly basis and I'll do a weekly backup before I do an update / upgrade

Being new I thought it might be some sort of electronically generated key file which had been lost when the data on the boot partition files went missing.

In my previous question, I mentioned the certificate issue with my domain name; another issue I have been getting is that typing in my domain name will take me to my router's login. This also happened when I was in Thunderbird and tried to send an email through the raspberry pi; upon hitting send, it gave me the security problems and also brought up my router's login pane. Any ideas here? I am using an ASUS RT-N66R running the latest Asus-Merlin firmware.

Hi,

I have had it running now for some months, and it really works like a charm. Thank you very much!!

Now I have started thinking about giving the rest of the family access and personal mail accounts as well, but prior to that i would like to move the home directories to a USB disk instead of the more limited SD card storage. Do you have any tips regarding this?

Cheers

The first step is about backing up your current SD card on your laptop so you won't lose any data, it all gets transferred to the USB drive. It might take longer if you have a large SD card (the blank space in the partition is copied too) but you can minimise that by shrinking the partition on your laptop so there's no empty space before you make the backup. Sam

Hi, first of all thank You very much for this tutorial. Secondly is there a way to make mail save not in /home/"user"/Maildir but make use of PostgreSQL or MySQL and save them in DB also with user information.

Hi I'm sure it's possible but it's beyond the scope of this tutorial and I haven't done it myself so I can't provide any instructions. I've tried to keep it simple here because the expected use case is a couple of users on a home server, so I think storing mail in a database would be overkill. The Postfix/Dovecot documentation is pretty good and may have what you're looking for. Good luck! Sam

Thanks for the response. Yes it is very important to do backups - especially with SD cards and the potential for power glitches to corrupt the memory. I actually have UPS protection on all 12 of my working pi's.

I've followed your tutorials and have owncloud and email-server pi's up and running - works great. I bought my domain name from namecheap and have a Comodo PositiveSSL certificate.

Here is what I'm trying to do. The 12 pi's are at different locations. The ten "non-server" pi's do various things (webcams, openelec, weather station, junk call blocker, etc.). They back up themselves with ssh and currently send email using cron jobs (through a gmail account), as to their status using ssmtp and mailutils following the instructions from http://iqjar.com/jar/sending-emails-from-the-raspberry-pi/. I'm trying to replace using the gmail account with my own pi email server accounts, but have been unsuccessful. I also send email with attachments using mpack.

Where I am having the problem is in the settings for (using my pi email-server) for host and port in .msmtprc (currently host smtp.gmail.com and port 587) and mailhub in /etc/ssmtp.ssmtp.conf (mailhub=smtp.gmai.com:587).

So, how can I easily send email from the command line on all these other pi's using the mydomain.com pi email server?

Thanks!

Add new comment

The content of this field is kept private and will not be shown publicly.

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.