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:
- An email server that you can run 24/7/365 for under £5 of electricity per year
- 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)
- The ability to connect from anywhere, and read & send email, using a secure IMAP connection on your phone, tablet or computer
- Log in to webmail using any web browser on a secure HTTPS connection, read & send email
- Complete control over your personal communication. Your emails are stored on YOUR server, and nobody is scanning them to sell you adverts.
- Smart spam filtering with Spamassassin
- Customisable mail sorting with Sieve rules
Postfix, the Mail Transfer Agent
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
Dovecot is used for two things:
- It provides you with IMAP functionality
- 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 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 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
Can't send from squirrelmail
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,
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 your full email address as the username?
Excellent!
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!!
Congratulations
RoundCube
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 !
Which pi?
Pi 2
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 : )
Duplicate email
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?
That's a new one!
/var/log/mail.log
)? SamUser error
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!
Thanks for posting your fix
Getmail to dovecot
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,
Hi 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
Have you set up a SPF record?
Mail Server Passwords Issue
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,
Revocery
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.
Another Question about my router
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.
Do you have ports 80 and 443
Moving home directory
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
Move the whole root FS instead
Interesting!
Interesting!
However - the disk I have in mind to move to is already half occupied. Any possibilities to respect that? Or woùld I have to wipe it clean?
The first step is about
Adding users and storing them in DB
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.
Yes but it's beyond the scope of this tutorial
Send email through email server - command line.
I've been successfully using the pi email server for quite some time.
Is there an easy way for a pi to send, from the command line, an email through the pi email server?
Scripting email
Sending email command line without MTA
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