How to Install an Email Server on your Raspberry Pi? –

When you’re a geek, you can do it all. Playing games, watching movies, and even writing software. Next to your day job, your one true passion is your computer and network of devices, which is why today we’re going to show you how to install an e-mail server on your Raspberry Pi.

If you’re looking for a cheap and easy way to setup your own email server, you may be interested in setting up a Raspberry Pi. This is a single-board computer that is pretty powerful and comes in different versions, one of which is a full-blown Linux computer. Another version comes with a dedicated GPU that enables the Pi to boot up Linux with a GUI. The real benefit of this version, however, is the ability to run a number of operating systems at once.

Many projects require the ability to send email, but setting up a mail server can be a project in itself.
So we will look at the different steps of configuring a web server, whether it is a simple SMTP or a full webmail.

Postfix is the main service that needs to be installed on the Raspberry Pi to host the mail server. It can send and receive emails.
Other services can then be added, such as. B. Dovecot to support POP/IMAP, and Roundcube can be used as webmail.

In this step-by-step guide, we will see how to configure everything.

If you want to get familiar with the Raspberry Pi, you can read my ebook here. This is a 30-day challenge where you will learn something new every day until you are a Raspberry Pi expert. The first third of the book explains the basics, and the following chapters present projects that you can try out for yourself.

Condition

If you want to set up an SMTP server, there are almost no requirements
A Raspberry Pi and an SMTP server that acts as a relay (such as Gmail) are sufficient.

If you want to take this class to the end, you will need:

  • Raspberry Pi
  • Domain name (I will use domain.com in all subsequent steps, don’t forget to change it)
  • Static public IP address (or at least a dynamic DNS service)

Note that I am doing this tutorial on a Raspberry Pi OS, so I recommend that you install the Raspberry Pi OS first (Lite is sufficient) to follow this
tutorial. I suggest you use SSH to follow this tutorial from your usual computer and copy/paste the commands and configurations (lots of them!).

Safety warning

Creating a secure mail server is not an easy task
It is easy to ignore the configuration and make your server an SMTP relay open to the world, or receive spam from spammers.

So follow these instructions carefully, and then keep an eye on the system logs to make sure you are the only one performing the actions taking place on your
server. Installing additional security features such as a firewall or fail2ban service may also be a good idea.

DNS configuration

IP address

In the next steps we change the DNS settings of our domain name to use our IP address as the mail server.

If you do not have a static public IP address, you will need to use a free dynamic DNS service such as No-IP to redirect the domain to your dynamic
IP address. You need to set up a tool to periodically give them your current IP address, and they will redirect the domain, for example. B. myserver.ddns.net, to your last known IP address (read more here).
If you don’t have a domain name, you can also use this alias directly

This is not ideal for a mail server, as you will have downtime when changing IP addresses, but if you don’t take your email too seriously, this shouldn’t be a problem.

Raspberry Pi Bootcamp
sale: 10% off today.
Take it to the next level.
I’m here to help you get started with your Raspberry Pi.
Learn all the necessary skills in the right order.

DNS zone configuration

You now need to go to your domain name registrar and change these zones to match your current IP address (or the domain name of your dynamic DNS provider):

  • MX
  • pop.domain.com
  • smtp.domain.com
  • imap.domain.com
  • mail.domain.com

MX is required to receive emails on your Raspberry Pi

The others are just easy to remember names to access your emails.

It may take up to 24 hours to apply the changes
You can make the changes using an online utility such as. B. Monitor Network-Tools.com
Select DNS from the Tools drop-down list and enter the domain or subdomain you want to monitor.

Installing Postfix to send email

Now let’s go to the heart of the matter, and install Postfix

Postfix will be the backbone of our mail server.
This will allow us to send and receive emails that correspond to our
domain name In this step, we will see how to send emails

Installation

First install the postfix package:
sudo apt install postfix

During installation, you must select these two configuration options:

  • General Post Configuration Type : Webpage
  • System mail name: domain.com

We will now make two changes to the generated configuration:

  • Open the configuration file
    sudo nano /etc/postfix/main.cf
  • Disable IPv6 support
    • Replaced by:
      inet_protocols = all
    • C:
      inet_protocols = ipv4
  • Enter your domain name as myhostname:
    myhostname= domain.com
  • If you are on a local network, most ISPs do not allow you to send email directly
    Therefore, you may need to add a relay host to your configuration
    Ask your ISP which server you can use as a relay:
    relayhost = smtp.yourprovider.com
  • Save and Exit (CTRL+O, Enter, CTRL+X)
  • Restart Postfix
    sudo service postfix restart

At this point, the server should boot properly with no
boot errors. If not, try to solve the following problems before continuing

Examination

We will now run our first test by sending an email from the Raspberry Pi

Telnet

For this test we will use telnet to connect to postfix

  • install telnet
    sudo apt-get install telnet
  • Connecting to SMTP server
    telnet localhost 25
  • Enter the following command sequence
  • This command sequence creates an email and sends it to [email protected] (external email address).

Here’s the full directions:

[email protected]:~ $ telnet localhost 25
Attempt ::1…
Try 127.0.0.1…
Connect to localhost.
The escape character is ‘^]’.
220 domain.com ESMTP Postfix (Raspbian)
ehlo domain.com
250-domain.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-DSN
250 SMTPUTF8
mail from: [email protected]
250 2.1.0 Ok
rcpt to: [email protected]
250 2.1.5 Ok
data
354 End of data with.
Subject:
Test
.
250 2.0.0 Ok : Standby under 44EAE1FE54
quit
221 2.0.0 Bye

Mailutils

If you are looking for the most user-friendly way to do this, you can install mailutils to use the command mail

  • install mailutils
    sudo apt install mailutils
  • Send a test mail with the command mail
    echo ‘Test’ | mail -s Test mail [email protected]

In both cases you can follow the sending of e-mails in this log file: /var/log/mail.log

Troubleshooting

In any case, you can check the /var/log/mail.log file to see what happened if you didn’t receive the email.
If everything is working properly, you should see something like this:
Jul 1 04:14:32 raspberrypi postfix/local [5433] : 734AA1FF96 : to=, relay=local, delay=0.09, delay=0.01/0.04/0.04, dsn=2.0.0, status=sent (delivered to mailbox)

Receiving email with Postfix

Now it’s time to modify the Postfix configuration to receive emails

Configuration

To do this, we use the Maildir
mailbox format, which is a simple and secure way to store email: each mailbox is a folder and each email is a file.

  • Edit the configuration file
    sudo nano /etc/postfix/main.cf
  • Add these lines to the end of the file
    home_mailbox = Maildir/
    mailbox_command =
    This configuration tells Postfix to create a Maildir folder for each user on the system
    Your new incoming mail will now be stored in this folder.

Now we need to create a maildir folder template by following the next steps:

  • Install these packages
    sudo apt install dovecot-common dovecot-imapd
  • Create a directory in the template directory
    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/.Cart
    sudo maildirmake.dovecot /etc/skel/Maildir/.Templates

These templates are used when adding new users to the Raspberry Pi.
But for those that already exist, you have to do it manually.

For pi you need z. B. run the following commands:
sudo cp -r /etc/skel/Maildir /home/pi/
sudo chown -R pi:pi /home/pi/Maildir
sudo chmod -R 700 /home/pi/Maildir

Master your Raspberry Pi in 30 days
Sale: 10% off today. Download
eBook.
Unlock the secrets of the Raspberry Pi in 30 days.
Learn useful Linux skills and practice some projects.

Examination

Now you can repeat the same test as before, but put the user pi in the recipient:
echo Test | mail -s Test [email protected]

And then check if the mail is in the Maildir folder

[email protected]:~ $ cat /home/pi/Maildir/new/1625109614.Vb302I205f1M127492.raspberrypi
Return-Path :
X-Original-To: [email protected]
Delivered-To: [email protected]
Received: from rpi.tips (Postfix, from userid 1000)
id 1CC5A205F2; Thu, 1 Jul 2021 04:20:14 +0100 (BST)
Subject: Test Mail Command
To: [email protected]
X-Mailer: mail (GNU Mailutils 3.5)
Message-Id: [email protected]
Date : Thu, 1 Jul 2021 04:20:14 +0100 (BST)
From: [email protected]
Test

You should only have one email in the new folder, use the auto-complete tabs to find it
As you can see, the address in the sender path is not correct, you need to change the hostname to correct this:
sudo hostname domain.com

But we have reached our goal at this point.
We receive emails that are sent to our domain

Mail Server Protection

As we said at the beginning, there are several ways to secure your web server minimally

  • Modify your configuration file
    sudo nano /etc/postfix/main.cf
  • Add these lines to the end of the file:
    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
  • This configuration limits the use of SMTP to the local network and rejects people claiming to be from your domain name.
  • Create a helo_access file:
    sudo nano /etc/postfix/helo_access
    In this file we need to enter the list of domain names to be blocked
  • Insert these lines
    X.X.X REJECT
    domain.com REJECT
    smtp.domain.com REJECT
    mail.domain.com REJECT
    Replace X.X.X with your public IP address
  • Restart the postfix daemon:
    sudo service postfix restart

Configuring Dovecot to allow POP and IMAP connections

We now have a working and secure
mail server. We will now continue with the next part, namely making this mail server accessible for POP and IMAP clients via SASL authentication.

As you may have noticed, in the previous step we already installed Dovecot to create the Maildir
folders.

Configuration

  • Open the configuration file of Dovecot:
    sudo nano /etc/dovecot/dovecot.conf
  • Removal of support for IPV6
    • Replace with:
      #lists = *, : :
    • C:
      listen = *
  • Open the Dovecot mail configuration file:
    sudo nano /etc/dovecot/conf.d/10-mail.conf
  • Modify the Maildir directory
    • Replace with :
      mail_location = mbox:~/mail:INBOX=/var/mail/%u
    • C:
      mail_location = maildir:~/Maildir
  • Open the Dovecot
    master configuration file sudo nano /etc/dovecot/conf.d/10-master.conf
  • Tell Dovecot to listen for SASL authentication
    • Comment out all the lines in the section on authenticating the default service (insert a # for each line).
    • Add these lines to the end of the file
      service auth {
      unix_listener /var/spool/postfix/private/auth {
      mode = 0660
      user = postfix
      group = postfix
      }
      }
  • Open the Dovecot configuration file for authentication:
    sudo nano /etc/dovecot/conf.d/10-auth.conf
  • Allow clear text authentication
    • Uncomment and edit this line:
      #disable_plaintext_auth = yes
    • To become this:
      disable_plaintext_auth = no
  • Also change this line:
    auth_mechanisms = simple login
  • Edit the postfix configuration file:
    sudo nano /etc/postfix/main.cf
  • Tell Postfix to use SASL (add these lines):
    smtpd_sasl_type = dovecot
    smtpd_sasl_path = private/auth
    smtpd_sasl_auth_enable = yes
  • Restart dovecot and Postfix:
    sudo service postfix restart
    sudo service dovecot restart

Keep an eye on the log files and you can use sudo service X status to check if the service is working properly. Like I said, a bad configuration can happen quickly. But now we’re going to do another test to make sure everything is okay.

Examination

To check if SASL authentication works properly, we create a test user and try to connect to the mail server with that user.

Create user

Create a new user with the login test and the password
sudo adduser test

Answer (and remember) the password questions. The other questions are optional (press Enter to skip them).

Receive encrypted password

We need to get our password encoded in base64 format as
You can get it with this command
printf ‘%s%s’ ‘[LOGIN]’ ‘[PASSWORD]’ | openssl base64

Replace [LOGIN] and [PASSWORD] in this command with the ones you selected with adduser.
In my case (test/password), the string AHRlc3QAcGFzc3dvcmQ= is displayed.

Application

Now we can try the telnet connection again by typing this string before the
ID The only change is that we have to use the AUTH PLAIN command to connect:
telnet localhost 25
ehlo rpi.tips
AUTH PLAIN

You can unsubscribe afterwards if you get the message Authentication succeeded, or send another test mail like the first time if you have doubts.
Here’s the full directions:

[email protected]:~ $ telnet localhost 25
Attempt ::1…
Try 127.0.0.1…
Connect to localhost.
The escape character is ‘^]’.
220 domain.com ESMTP Postfix (Raspbian)
ehlo domain.com
250-domain.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-AUTH PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
AUTH PLAIN AHRlc3QAcGFzc3dvcmQ=
235 2.7.0 Authentication successful
mail from: [email protected]
250 2.1.0 Ok
rcpt to: [email protected]
250 2.1.5 Ok
data
354 End data with.
Subject:
Test
.
250 2.0.0 Ok : Standby under 44EAE1FE54
quit
221 2.0.0 Bye

Enable IMAPS

Dovecot allows us to connect through IMAP (telnet localhost 143)
But now we need to enable TLS for IMAP on port 993

  • Modify the Dovecot
    master configuration file sudo nano /etc/dovecot/conf.d/10-master.conf
  • Enable the listener on port 993The configuration should look like this (some lines need to be commented out):service imap-login {inet_listener imap {port = 143}inet_listener imaps {port = 993ssl = yes}
  • Then edit the SSL configuration file:
    sudo nano /etc/dovecot/conf.d/10-ssl.conf
  • Make sure SSL is enabled at the beginning of the file:
    ssl = yes
  • The certificate location must also be unchecked:
    ssl_cert = </etc/dovecot/private/dovecot.pem
    ssl_key = </etc/dovecot/private/dovecot.pem
  • Finally, restart the dovecot
    server sudo service dovecot restart

Now you can check if your IMAPS server is working with the following command
openssl s_client -connect localhost:993

The connection syntax is as follows:
a login [LOGIN] [PASSWORD].

A full track should look like this:

* OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE AUTH=PLAIN AUTH=LOGIN] Dovecot is ready.
a login pi password
a OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE SORT SORT=DISPLAY THREAD=REFERENCES THREAD=REFS THREAD=ORDEREDSUBJECT MULTIAPPEND URL-PARTIAL CATENATE UNSELECT CHILDREN NAMESPACE UIDPLUS LIST-EXTENDED I18NLEVEL=1 CONDSTORE QRESYNC ESEARCH ESORT SEARCHRES WITHIN CONTEXT=SEARCH LIST-STATUS BINARY MOVE SPECIAL-USE] Joined
b select inbox
* FLAGS (Answered Flagged Deleted Seen Draft)
* OK [PERMANENTFLAGS (Answered Flagged Deleted Seen Draft *)] flags are allowed.
* 3 PRESENT
* 0 RECENT
* OK [NOT SEEING 1] First time not seen.
* OK [UIDVALIDITY 1536038369] Valid UID
* OK [UIDNEXT 4] Prediction of next UID
b OK [READ-WRITE] Selection completed (0.000 + 0.000 sec.).
b Output
* BYE Output
b OK Selection completed (0.000 + 0.000 sec.).
closed

You can now connect to your IMAP server from any client on the local network
If you want to access your server from anywhere, remember to open the necessary ports in your router’s firewall.

Configure roundcube to add webmail access

Most of the work is done, but we’re taking it a step further and adding Roundcube, a leading free and open source webmail software, to our mail server on the Raspberry Pi
.

The main advantage of Roundcube over other webmailers is that it is directly available in the Debian repositories and thus in the Raspberry Pi OS.

If you started with a blank Raspberry Pi operating system, you must first install the MySQL server (MariaDB).

MySQL Server

If you do not already have one, you must install a MySQL server to store the Roundcube database:
sudo apt install mariadb-server

Next, the following steps are required to set the root password and create a roundcube user:

  • Log in as root (we need sudo because only root has access):
    sudo mysql -uroot
  • Set root password
    Use mysql;
    UPDATE user SET password=PASSWORD(‘YourPassword’), plugin=” WHERE User=root AND Host=localhost;
    FLUSH PRIVILEGES;
    Don’t forget to replace yourPassword with a strong password.
  • Create a new user for Roundcube :
    CREATE USER ’roundcube’@’localhost’ IDENTIFIED BY ‘password’;
    Replace password with the password you want.
  • Create a roundcube database
    CREATE DATABASE roundcubemail ;
  • Grant the Roundcube user all rights to the Roundcube database:
    DELIVER ALL PRIVILEGES AT roundcubemail.* to ’roundcube’@’localhost’ ;
  • Exit the MySQL console:
    FLUSH PRIVILEGES;
    exited.

Your database server is ready, go to the next step.

Round cube

To install it, type the following command:
sudo apt install roundcube roundcube-plugins

This will also install all other dependencies (mainly Apache, PHP and the MySQL client) automatically.

Again, the installation wizard will ask you these questions about your MySQL server:

  • Configure the database with dbconfig-common: yes
  • MySQL application password for Roundcube : Your Roundcube user password
  • Password of the database administrator : Your MySQL root password

Message: At this point, a connection error occurred, but when I tried again, I was able to change all the settings, including the database name, and then everything was fine.

Now edit the Apache configuration for Roundcube to enable the web application:
sudo nano /etc/apache2/conf-enabled/roundcube.conf

Comment on the first line:
Alias /roundcube /var/lib/roundcube

Then restart your web server:
sudo service apache2 restart
And go to http://[RASPBERRY-IP]/roundcube to see the web interface.

If you get errors, you can restart the installation wizard with this command:
sudo dpkg-reconfigure roundcube-core
This will also give you the option to install the default server if you wish.

You can now login with the credentials you created in the previous step or with your pi
account. Now enjoy your webmail, and don’t forget that you can add many plugins to RoundCube to expand its capabilities.

Configuration file logs and summary

So we looked at how to set up a full mail server on the Raspberry Pi
. If you have any errors or would like to continue, here is a brief description of the location of the files

Postfix

In this tutorial we will use Postfix to send and receive emails, it is the heart of the mail server.

Configuration

  • /etc/postfix/main.cf : Basic configuration for Postfix
  • /etc/postfix/master.cf : Process configuration for Postfix

Log files

  • / var/log/mail.log : Here you can see all traces of the mails, as well as any errors.

Pigeon loft

We used Dovecot to manage IMAP connections with SASL security.

Configuration

  • /etc/dovecot/dovecot.conf : Dovecot basic configuration
  • /etc/dovecot/conf.d/ : This sub-folder contains several files with each part of the configuration, so you can easily see where the option you need is located.

Log files

  • /var/log/syslog : Dovecot does not have its own log file, it uses the main syslog file

Apache

In this tutorial, Apache is used to run Roundcube
. Normally you do not need to change anything, unless Roundcube is not available at all.

Configuration

  • /etc/apache2/apache2.conf : Basic configuration file for apache2
  • /etc/apache2/conf-enabled/ : Here you can find the configuration of some Apache services (for example Roundcube.conf).
  • /etc/apache2/sites-enabled/ : You can find the configuration for each Apache website here

Log files

  • /var/log/apache/error.log : If you encounter errors while working with Apache, you can find them here

Round cube

Finally, we installed Roundcube to add the webmail to our mail server.

Configuration

  • /etc/roundcube/config.inc.php : Here is the basic configuration file for Roundcube

Log files

  • /var/log/roundcube/errors : If you have problems with Roundcube, you will find errors in this file

Supplement

And that’s it, we have come to the end of this tutorial
You have learned how to set up a full mail server with :

  • Postfix for transport
  • Dovecot for secure authentication
  • Roundcube for a web-based access to your e-mails

As you may have noticed, this is not an easy thing to set up, there are still quite a few configuration options, and it can be a lot of work to set it up at home.

I think in most cases, the first part with Postfix is what you are interested in
you will be able to send emails from different projects, but not necessarily implement all the other steps

Anyway, if you really need to install everything, you know how to do it

Frequently Asked Questions

How do I setup an email server on my Raspberry Pi?

You can use the following guide to setup an email server on your Raspberry Pi. How do I setup a VPN server on my Raspberry Pi? You can use the following guide to setup a VPN server on your Raspberry Pi.

How do I get my own email server?

See the email server documentation.

How do I setup a local mail server?

A local mail server is a mail server that is installed on a computer that is physically on your network. You can setup a local mail server on a computer that is running Windows, Mac OS X, or Linux. A local mail server can be used to send and receive email on your network.

Related Tags:

raspberry pi email server 2020raspberry pi exchange serverraspberry pi email server 2021rpi email servercitadel email server raspberry piraspberry pi smtp server,People also search for,Feedback,Privacy settings,How Search works,Citadel/UX,Postfix,SquirrelMail,Sendmail,Exim,Zimbra,See more,raspberry pi email server 2020,raspberry pi exchange server,raspberry pi email server 2021,rpi email server,citadel email server raspberry pi,raspberry pi smtp server,raspberry pi email server reddit,raspberry pi sendmail

Leave a Reply

Your email address will not be published. Required fields are marked *