Raspberry Pi Email Server Part 1: Postfix

Powered by Drupal
Submitted by Sam Hobbs on

Postfix Logo This is the first part of a five part tutorial that will show you how to install a full featured email server on your Raspberry Pi. This tutorial covers Postfix, the Mail Transfer Agent.

The parts are:

The Introduction & Contents Page (read first)

Raspberry Pi Email Server Part 1: Postfix

Raspberry Pi Email Server Part 2: Dovecot

Raspberry Pi Email Server Part 3: Squirrelmail

Raspberry Pi Email Server Part 4: Spam Detection with Spamassassin

Raspberry Pi Email Server Part 5: Spam Sorting with LMTP & Sieve

Installing Postfix

Note: While you are setting up the mail server on the Pi, it’s a good idea to turn off port forwarding rules for email to the Pi in your router’s firewall. If you don’t have any port forwarding rules now, that’s great, don’t worry – I’ll prompt you to set them up later. First, log into your Pi with a SSH session and install postfix:

sudo apt-get update
sudo apt-get install postfix

You will see a menu with some choices. Select “Internet Site” and then set the mail name to your domain name, not including www. (e.g. samhobbs.co.uk). The setup script will then do some automatic configuration for you. The output will look something like this:

Selecting previously unselected package postfix.                                              
(Reading database ... 67653 files and directories currently installed.)                       
Unpacking postfix (from .../postfix_2.9.6-2_armhf.deb) ...                                    
Processing triggers for man-db ...
Setting up postfix (2.9.6-2) ...
Adding group `postfix' (GID XXX) ...
Done.
Adding system user `postfix' (UID XXX) ...
Adding new user `postfix' (UID XXX) with group `postfix' ...
Not creating home directory `/var/spool/postfix'.
Creating /etc/postfix/dynamicmaps.cf
Adding tcp map entry to /etc/postfix/dynamicmaps.cf
Adding sqlite map entry to /etc/postfix/dynamicmaps.cf
Adding group `postdrop' (GID XXX) ...
Done.
setting myhostname: samhobbs
setting alias maps
setting alias database
changing /etc/mailname to samhobbs.co.uk
setting myorigin
setting destinations: samhobbs.co.uk, samhobbs, localhost.localdomain, localhost
setting relayhost: 
setting mynetworks: 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
setting mailbox_size_limit: 0
setting recipient_delimiter: +
setting inet_interfaces: all
/etc/aliases does not exist, creating it.
WARNING: /etc/aliases exists, but does not have a root alias.

You can edit all of this later. You may also get some warnings like this:

postmulti: warning: inet_protocols: disabling IPv6 name/address support: Address family not supported by protocol

IPv6 is a new type of IP address that was introduced because we’re running out of the “old” IPv4 addresses. Not many ISPs support IPv6 yet, so you probably don’t need it. Unless you fix the warning, you’ll see it every time. Change directory into the postfix configuration folder:

cd /etc/postfix/

Edit /etc/postfix/main.cf with your favourite command line text editor (e.g. sudo nano main.cf) and add inet_protocols = ipv4 to the end of the file. Now is also a good time to check that your hostname is specified properly in /etc/postfix/main.cf. The setup script takes the hostname of the server and uses that, but it may not be in the right format, i.e. “samhobbs” instead of “samhobbs.co.uk”. Find the line that begins myhostname = and make sure it is your fully qualified domain name. This is important because your server will use this to talk to other mail servers, and some will reject your emails if you don’t use a fully qualified domain name to say hi! This is covered in more detail in the helo access restrictions later. Restart postfix and you shouldn’t see the warnings any more:

sudo service postfix restart

Testing and Configuration

Before you start, it’s probably worth backing up the configuration files in their current state. This way, you’ll have something to compare to if you’re ever trying to work out which bits were defaults and which bits you changed yourself:

cd /etc/postfix
sudo cp main.cf main.cf.BAK
sudo cp master.cf master.cf.BAK

Mailbox Setup

There are a couple of different types of mailbox you can use, I’ve chosen to use a “Maildir” rather than “mbox” configuration. For users with “real” UNIX accounts on the system (like the one you’re using to log in), Maildir creates a folder in the user’s home directory and places emails inside it, one file for each email. I prefer this to the alternatives, because it’s easier to see and understand: you can rummage around in your home folder and see all your emails as individual files. To tell Postfix to use the Maildir format, add the following lines to /etc/postfix/main.cf:

home_mailbox = Maildir/
mailbox_command =

If there's already a line with mailbox_command, comment it out by adding a # at the start of the line. We also need to create the mail directory and its subfolders for existing users, and add some things to /etc/skel (the template for new users) so that if you create a new account this will be done automatically. These commands are part of Dovecot, so first we need to install it:

sudo apt-get update
sudo apt-get install dovecot-common dovecot-imapd

You will get a lot of output: some other dovecot packages will automatically be installed and the config files will be created. You will also see some errors – don’t worry about those for now, I’ll explain how to deal with them in part 2, later. Now we can create those mail folders. Run the following commands to create the template files:

sudo maildirmake.dovecot /etc/skel/Maildir
sudo maildirmake.dovecot /etc/skel/Maildir/.Drafts
sudo maildirmake.dovecot /etc/skel/Maildir/.Sent
sudo maildirmake.dovecot /etc/skel/Maildir/.Spam
sudo maildirmake.dovecot /etc/skel/Maildir/.Trash
sudo maildirmake.dovecot /etc/skel/Maildir/.Templates

Next, copy the files over to existing users’ home directories, and change the ownership and permissions for privacy (replace USER with the username you are doing this for, and repeat for all existing usernames):

sudo cp -r /etc/skel/Maildir /home/USER/
sudo chown -R USER:USER /home/USER/Maildir
sudo chmod -R 700 /home/USER/Maildir

Initial Testing

Now, the best way to test Postfix during configuration is to use Telnet, because it is such a simple way of communicating between programs and there’s less to go wrong and get confused about. First, install telnet:

sudo apt-get install telnet

Now, still inside the SSH session to your pi, type this command. It will connect you to port 25 on the Pi:

telnet localhost 25

You can now test sending an email using SMTP. Here are the steps:

  1. send an ehlo command to tell the server who you are, and it will tell you its capabilities
  2. use the mail from command to say who the email is from. If you are sending it from an address that exists on the server, you needn’t include the domain name (i.e. user instead of user@yourdomain.com)
  3. use the rcpt to command to tell the server where to send the email
  4. Use the data command to tell the server that you’re about to start giving it the message you want to send
  5. Type Subject: YOUR SUBJECT then enter to set a subject
  6. Type the body of your email. Once you’re done, press ENTER, then ., then ENTER again.
  7. Type quit to exit

Here’s an example:

telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 samhobbs.co.uk ESMTP Postfix (Debian/GNU)
ehlo foobar
250-samhobbs.co.uk
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
mail from: me        
250 2.1.0 Ok
rcpt to: me@outsideemail.com
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
Subject: test
This is a test email
.
250 2.0.0 Ok: queued as A639C3EE6D
quit 
221 2.0.0 Bye

Some Access Restrictions

Add the following to /etc/postfix/main.cf to restrict who can send emails to external mail servers:

smtpd_recipient_restrictions =
        permit_sasl_authenticated,
        permit_mynetworks,
        reject_unauth_destination

Reload postfix:

sudo service postfix reload
  • Line 1 begins the list of restrictions.
  • Line 2 permits users who have authenticated with Simple Authentication and Security Layer (SASL) to send email to any destination (this is part of the Dovecot config in Part 2, later).
  • Line 3 will let users send emails to any destination if they have connected from an IP address defined in mynetworks.
  • Line 4 will reject the email if none of the above conditions have been met unless the “rcpt to” address is one of the addresses that your server is accepting email to (as defined in main.cf with the mydestination parameter).

In its present state, the email server will allow you to send external emails because the connection is originating from the Pi itself (you are logged in via SSH) and not an unknown computer. Addresses of “trusted” computers are listed under the mynetworks setting in main.cf, e.g.

mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128

Try sending an external email again, using telnet as before. You should be able to do so without any issues. Now we want to see what kind of response someone would get if they were connecting from outside of the IP range defined in mynetworks, to make sure Pi won’t allow everyone to send outgoing emails from your server. To simulate this we can comment out permit_mynetworks under smtpd_recipient_restrictions:

smtpd_recipient_restrictions =
        permit_sasl_authenticated,
#       permit_mynetworks,
        reject_unauth_destination

Now reload the postfix configuration:

sudo service postfix reload

This will let you see what kind of response you would get if you weren’t sending the email from mynetworks. Try sending again, and you should receive an error “554: Relay access denied“:

admin@samhobbs /etc/postfix $ telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 samhobbs.co.uk ESMTP Postfix (Debian/GNU)
ehlo samhobbs.co.uk
250-samhobbs
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
mail from: USER
250 2.1.0 Ok
rcpt to: me@externalemail.com
554 5.7.1 <me@externalemail.com>: Relay access denied
quit
221 2.0.0 Bye
Connection closed by foreign host.

Perfect. Leave permit_mynetworks commented out in your smtpd_recipient_restrictions (you'll see why in part 2).

Helo access restrictions

Helo access restrictions can be a very useful way of blocking spam. Note that we’re not talking about unauthorised people being able to send email outside your network any more (that’s taken care of with the smtpd_recipient_restrictions); we’re now talking about stopping spammers from sending incoming mail to your email address. Spammers try to conceal their identity so that they don’t end up on block lists, so they rarely use helo hostnames that could identify them – these hostnames are written to the mail log files. As a result, they often make up a random string or use an IP address instead of a domain name. Luckily, these are easily taken care of. Add the following to /etc/postfix/main.cf:

smtpd_helo_required = yes
smtpd_helo_restrictions =
        permit_mynetworks,
        permit_sasl_authenticated,
        reject_invalid_helo_hostname,
        reject_non_fqdn_helo_hostname,
        reject_unknown_helo_hostname
  • Line 1 requires people and programs to identify themselves when they send email, using the helo or ehlo commands I mentioned earlier.
  • Line 2 starts the list of restrictions.
  • Line 3 accepts any old rubbish in the ehlo if it comes from an IP address defined in mynetworks. If the connection isn’t connecting from an IP address in mynetworks, then the helo hostname is checked against the rest of the list.
  • Line 4 accepts any helo hostname if the client is authenticated with SASL (I added this to the tutorial recently after troubleshooting problems some people had in the comments – it allows you to connect from any network and still send messages through your Pi. Mobiles will usually work without this because most providers pass mail through their own proxies, so your Pi receives a connection from the proxy – which has a valid hostname – and not from the mobile, which may be called something like “android-b627cfe2efea7e67″).
  • Line 5 rejects connection attempts when the HELO hostname syntax is invalid.
  • Line 6 rejects non-fully qualified domain names (for example, foobar instead of foobar.com). This will also block those random strings, e.g. “kjhrsbvks”.
  • Line 7 rejects the helo hostname if it that domain doesn’t have a valid DNS A or MX record. For example, someone spamming you could make up a domain like theflyingspaghettimonster.com. If that domain doesn’t actually exist and have the right records, then your server won’t accept it as a hostname, and the email will be rejected.

If the helo hostname gets past line 7 and hasn’t been denied, it is accepted. You’d be surprised how much spam these helo access restrictions will block on their own (looking through my log files, I can see numerous spam scripts that have attempted to ehlo with my IP address), but there’s an extra step we can add in here to help:

Blocking people claiming to be your domain name

Many spammers try to send email to you after helo’ing with your own domain name. Since postfix doesn’t check whether or not they’re lying about their helo hostname, this will usually work. But, since we’ve put permit_mynetworks at the top of the list, anyone actually sending an email from your domain will be accepted already. Anyone using your hostname who isn’t in mynetworks is an imposter. So, add one more line to the end of the restrictions list:

smtpd_helo_restrictions =
        permit_mynetworks,
        permit_sasl_authenticated,
        reject_invalid_helo_hostname,
        reject_non_fqdn_helo_hostname,
        reject_unknown_helo_hostname,
        check_helo_access hash:/etc/postfix/helo_access

That last line checks a file for custom rules you’ve built in. Create the file:

sudo nano /etc/postfix/helo_access

Add the following lines, edited for your domain:

samhobbs.co.uk          REJECT          Get lost - you're lying about who you are
mail.samhobbs.co.uk      REJECT          Get lost - you're lying about who you are

Now tell postfix to map the file, and restart postfix:

sudo postmap /etc/postfix/helo_access
sudo service postfix restart

Now anyone who tries to ehlo with one of the hostnames you defined in that file gets rejected, and sees the “get lost” message. Your legitimate servers won’t have that problem, because they will already have been accepted higher up the list. Neat, right? I found that little nugget of wisdom at unixwiz.net.

Moving on…

We’re almost done with Postfix now, athough there are a few bits of configuration that we’ll want to do once we’ve set up SASL with Dovecot, which I’ve chosen to lump in with the Dovecot tutorial. In Raspberry Pi Email Server Part 2: Dovecot, we’ll set up Dovecot to provide SASL authentication and IMAP capability. Please leave a comment if you’re having trouble with anything in this tutorial, and I’ll try and help you out!

Comments

Hello again, Can you try sending a message to your own server, and post both sides of the "conversation" (relevant extracts from /var/log/mail.log) making it clear which part is from the sender (the new server you are setting up) and which part is from the recipient (your existing personal server)? Thanks

OK Sam, here goes:-

Sending email from personal computer to email account on mail server...

< REDACTED >

Sending email from email account setup on email server to personal computer...

< REDACTED >

Please note, I haven't disguised anything here in order to let you see exactly what's happening. Please don't post publicly. Thanks.

OK so the next thing is to check the mydestinations parameter on the new email server, and make sure yourorganisation.com is in there as well as mail.yourorganisation.com (otherwise the server won't think it's the destination server for foo@yourorganisation.com, only foo@mail.yourorganisation.com). Sam

Thanks Sam.

Added the domain to mydestinations in my /etc/postfix/main.cf file.

/var/log/mail.log returns the following when I attempt to send an email to postmaster@yourorganisation.com:-

Aug 8 18:42:35 blue postfix/smtpd[3850]: connect from yourdomain.com[82.69.17.139]
Aug 8 18:42:35 blue postfix/smtpd[3850]: 9E7D01F635: client=yourdomain.com[82.69.17.139]
Aug 8 18:42:36 blue postfix/cleanup[3853]: 9E7D01F635: message-id=<33623780-e806-b2c6-6853-cedc8b85cd81@member.fsf.org>
Aug 8 18:42:36 blue postfix/qmgr[3829]: 9E7D01F635: from=<you@member.fsf.org>, size=5759, nrcpt=1 (queue active)
Aug 8 18:42:36 blue postfix/smtpd[3850]: disconnect from yourdomain.com[82.69.17.139] ehlo=1 mail=1 rcpt=1 data=1 quit=1 commands=5
Aug 8 18:42:36 blue postfix/local[3854]: 9E7D01F635: to=<root@mail.yourorganisation.com>, orig_to=<postmaster@yourorganisation.com>, relay=local, delay=0.59, delays=0.56/0.01/0/0.01, dsn=2.0.0, status=sent (delivered to maildir)
Aug 8 18:42:36 blue postfix/local[3854]: warning: alias database loop for root
Aug 8 18:42:36 blue postfix/local[3854]: 9E7D01F635: to=<root@mail.yourorganisation.com>, orig_to=<postmaster@yourorganisation.com>, relay=local, delay=0.6, delays=0.56/0.01/0/0.03, dsn=5.4.6, status=bounced (alias database loop for root)
Aug 8 18:42:36 blue postfix/cleanup[3853]: 3BD931F647: message-id=<20190808174236.3BD931F647@mail.yourorganisation.com>
Aug 8 18:42:36 blue postfix/qmgr[3829]: 3BD931F647: from=<>, size=7829, nrcpt=1 (queue active)
Aug 8 18:42:36 blue postfix/bounce[3855]: 9E7D01F635: sender non-delivery notification: 3BD931F647
Aug 8 18:42:36 blue postfix/qmgr[3829]: 9E7D01F635: removed
Aug 8 18:42:38 blue postfix/smtp[3857]: 3BD931F647: to=<you@member.fsf.org>, relay=mail.fsf.org[209.51.188.13]:25, delay=2.5, delays=0.01/0.01/0.71/1.8, dsn=2.0.0, status=sent (250 OK id=1hvmQw-0000nN-DO)
Aug 8 18:42:38 blue postfix/qmgr[3829]: 3BD931F647: removed

Please omit any revealing details as usual.

Sorry for the wait, I've been really busy. It kind of looks like the email arrived and then a local delivery problem caused postfix to send a bounce notification. Two things:
  • If you check the mailbox for root, did the email actually arrive?
  • Can you post the contents of /etc/aliases
I have a hunch the "loop" postfix is complaining about is to do with how specific you have been about your aliases (root vs root@yourorganisation.com vs root@mail.yourorganisation.com). Your setup is a little bit more complicated than in the tutorial, distinctions that didn't matter then might now. Sam

Hi Sam. I'm having some confusion when following this tutorial. I'm very new to any form of Unix, and to the Raspberry Pi, but I recently bought a RPi4, and set it up with NOOBS. I've turned it into a webserver running one website. Now I want to add email, and I arrived at your tutorial, but things seem to have changed a little since it was written.

The first change I noticed was when installing dovecot. It installed but with the message, "Note, selecting 'dovecot-core' instead of 'dovecot common'. I don't know if that made a difference to what came immediately afterwards, when I tried to create the 5 files (.Drafts, .Sent, etc.) in /etc/skel/Maildir/. All 5 appeared to be created, because there were no error messages, but none of them can be seen, not even as root with hidden files showing. So it appears that they weren't created.

Earlier today, I went further through the tutorial but I ended up starting again. Right now there doesn't seem to be any point in me going any further because those 5 files weren't created, and especially as it was 'dovecot-core' that was installed instead of 'dovecot-common', so maybe carrying on through the tutorial simply won't work.

Any thoughts or ideas, Sam - or anyone?

Phil, Sorry for the delayed reply. Were the files not created in the /etc/skel/Maildir directory? I take it you are using "sudo ls -al /etc/skel/Maildir" to check? The files won't automatically be created in the home directories of existing users, but they should be created for new users. Sam

Wait! I just tried again and what I wasn't doing was including -a to see those hidden folders. I just tried to create .Drafts once again and it's there - along with other stuff I wasn't aware of. I'll continue through the tutorial and post how it works out.

I've got down to the bottom of page 1 (Postfix), and I need to ask a question before continuing, Sam.

The sending email tests. Are they supposed to actually reach their recipients? because, in the course of going through the Postfix page, I've sent 3 such test emails but none of them arrived at my external email address. If they are supposed to arrive, then something is wrong, and there's no point in continuing the installation/setup until whatever it is is discovered.

Ty, Sam.

I'd actually found my way to the mail log and discovered that the emails been sent but blocked down the line. As a test, today I set up another external email address and tried sending to that too. But that was also blocked. Two destinations, each using a different blocker. In both cases, it's the IP address that's blacklisted. The IP isn't actually a problem because I use it to send email on my main computer.

So I looked further with one of the blocking systems (Spamhaus) and discovered that my IP is on their Policy Block List. It's policy and not a history of spam. I simply need to turn SMTP authentication on to fix the problem. I imagine that doing that will also fix the other blocker. I don't yet know if that would be done by the end of the tutorial or not, as I've been out in the meantime and haven't yet read through the whole process. Which is what I'm about to do right now :)

So I can send internally but I am stuck on:

Jun 6 00:25:15 raspberrypi postfix/cleanup[23318]: 36A6A61CBB: message-id=<20200606052458.36A6A61CBB@ponderth.at>
Jun 6 00:25:15 raspberrypi postfix/qmgr[23309]: 36A6A61CBB: from=, size=332, nrcpt=1 (queue active)
Jun 6 00:25:15 raspberrypi postfix/smtp[23355]: warning: relayhost configuration problem
Jun 6 00:25:15 raspberrypi postfix/smtp[23355]: 36A6A61CBB: to=, relay=none, delay=60, delays=59/0.01/0.41/0, dsn=4.3.5, status=deferred (Host or domain name not found. Name service error for name=smtp.ponderth.at type=A: Host not found)
Jun 6 00:25:22 raspberrypi postfix/smtpd[23320]: disconnect from localhost[127.0.0.1] ehlo=1 mail=1 rcpt=1/2 data=1 quit=1 unknown=0/1 commands=5/7

gonna dig some more into this, because I think I am close to it working, but something is blocking it. I have not installed doevecot yet, just postfix and telnet, but that's all I would need to start with right?

Sam, this is an excellently written article on what is needed and how to set up a home email service which is really easy to follow for the total beginner like myself.
Unfortunately it seems I am failing at the first hurdle and am unable to send an email to my gmail account.
I have looked at the email logs and get the following:
Jun 23 12:34:04 raspberrypi postfix/smtpd[7380]: 60D303F669: client=localhost[::
1]
Jun 23 12:34:13 raspberrypi postfix/cleanup[7389]: 60D303F669: message-id=<20200
623113404.60D303F669@raspberrypi.home>
Jun 23 12:34:13 raspberrypi postfix/qmgr[3388]: 60D303F669: from=, size=335, nrcpt=1 (queue active)
Jun 23 12:34:20 raspberrypi postfix/smtpd[7380]: disconnect from localhost[::1]
ehlo=1 mail=1 rcpt=1 data=1 quit=1 commands=5
Jun 23 12:34:31 raspberrypi postfix/smtp[7390]: 60D303F669: to=, relay=gmail-smtp-in.l.google.com[2a00:1450:400c:c00::1b]:25, delay=47, delay
s=30/0.05/17/0.26, dsn=5.7.1, status=bounced (host gmail-smtp-in.l.google.com[2a
00:1450:400c:c00::1b] said: 550-5.7.1 [2a00:23c7:55aa:8701:9b5b:33b0:5a18:f689]
Our system has detected that 550-5.7.1 this message does not meet IPv6 sending g
uidelines regarding PTR 550-5.7.1 records and authentication. Please review 550-
5.7.1 https://support.google.com/mail/?p=IPv6AuthError for more information 550
5.7.1 . c14si9208812wrp.308 - gsmtp (in reply to end of DATA command))
Jun 23 12:34:31 raspberrypi postfix/cleanup[7389]: 1DD013F66D: message-id=<20200
623113431.1DD013F66D@raspberrypi.home>
Jun 23 12:34:31 raspberrypi postfix/bounce[7391]: 60D303F669: sender non-deliver
y notification: 1DD013F66D

Any help would be muchly appreciated.

Hi Sam,

Something that I can't remember seeing before:

postfix.service loaded active exited Postfix Mail Transport Agent
postfix@-.service loaded active running Postfix Mail Transport Agent (instance -)

The 2nd line with a postfix@-.service? Is it supposed to be there? Should I worry?

Please advise.

Regards,

Jo

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.