DEV Community

Sourav kumar
Sourav kumar

Posted on

Day -03 /100 - Configuring mail server using Postfix, Dovecot and Mutt

We will set up three things in ubuntu machine:-

  1. Postfix (It will provide SMTP Implementation, which will work as an email transfer agent - MTA)
  2. Dovecot (It will provide POP3/IMAP service, which will act as an email delivery agent.)
  3. Mutt (It will work as an email client and will provide a nice interface to write/send/receive emails.)

We will set up ubuntu server as an email server, hence we need to assign an adequate hostname and a fully qualified domain name.

To change the hostname, run the following command:

sudo hostnamectl set-hostname "mailserver-name"
Enter fullscreen mode Exit fullscreen mode

Image description

Open another terminal and you will see the hostname has changed.

Image description

Next, we will change the fully qualified domain name:

sudo nano /etc/hosts
Enter fullscreen mode Exit fullscreen mode

Image description

Go to the terminal and find your IP address by running - ifconfig (run command shown in image if "ifconfig" not found)

Image description

Image description

Copy the IP address and paste it in the "/etc/hosts" file

Image description

The hostname and fully qualified domain name are changed successfully.

Image description

Step 1 - Postfix Installation

sudo apt install postfix -y
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Change the system mail name from "mail.sourav.com" to "sourav.com" and press Enter.

Verify the installation of Postfix using the following command:

sudo systemctl status postfix
Enter fullscreen mode Exit fullscreen mode

Image description

Let's change the location in the configuration file where the emails will be saved.

We will create a folder "Maildir" and will give its path

sudo postconf "home_mailbox = Maildir/"
Enter fullscreen mode Exit fullscreen mode

Image description

Step 2 - "Dovecot" Installation

Run:

sudo apt install dovecot-imapd dovecot-pop3d dovecot-core
Enter fullscreen mode Exit fullscreen mode

Verify the installation using

sudo systemctl status dovecot
Enter fullscreen mode Exit fullscreen mode

Image description

To reload any service use this:

sudo systemctl reload dovecot
sudo systemctl reload postfix
Enter fullscreen mode Exit fullscreen mode

Let's change the location in the configuration file for dovecot also.

sudo nano /etc/dovecot/conf.d/10-mail.conf
Enter fullscreen mode Exit fullscreen mode

Comment the old mail location and uncomment the new one as shown in the image:
Image description

Check the changed location using:

doveconf -n
Enter fullscreen mode Exit fullscreen mode

Image description

Whenever a new user signs up on the mail server, we want the "Maildir" to be created automatically in the user's home directory. This "Maildir" will work as the Inbox for the user.

To achieve this, we will go to /etc/skel/

Note- Whatever we create in this directory whether a file or folder, gets copied to home when a new user signs up.

cd /etc/skel
sudo mkdir -p Maildir/cur Maildir/new Maildir/tmp
Enter fullscreen mode Exit fullscreen mode

All 3 folders are created.

Image description

Step 3 - Installing mutt

Run

sudo apt install mutt
Enter fullscreen mode Exit fullscreen mode

Now we want that if anybody signs up then he/she should have access to mutt so that they can get a nice interface to send/receive/write emails.

So we will make all configurations of mutt in the same location - /etc/skel

By doing this every user will get access to mutt without installing it.

sudo mkdir /etc/skel/.mutt
Enter fullscreen mode Exit fullscreen mode

Image description

We have created a hidden directory mutt.

Now go inside .mutt and create a file "muttrc"

cd .mutt/
sudo nano muttrc
Enter fullscreen mode Exit fullscreen mode

and paste this content

set imap_user = ""
set imap_pass = ""
set folder = imaps://mail
set spoolfile = +INBOX
set realname = ''
set from = "$imap_user"
set use_from = yes
set sort=reverse-date
mailboxes = INBOX
set timeout=1
set sidebar_visible = yes
source ~/.mutt/mutt_colors
Enter fullscreen mode Exit fullscreen mode

Now create another file mutt_colors using command

sudo nano mutt_colors
Enter fullscreen mode Exit fullscreen mode

and copy paste the content


# Colours for items in the index
color index brightcyan black ~N
# Hmm, don't like this.
# color index brightgreen black "~N (~x byers.world)|(~x byers.x)|(~x langly.levallois123.axialys.net)|(~x the.earth.li)"
color index brightyellow black ~F
color index black green ~T
color index brightred black ~D
mono index bold ~N
mono index bold ~F
mono index bold ~T
mono index bold ~D

# Highlights inside the body of a message.

# URLs
color body brightgreen black "(http|ftp|news|telnet|finger)://[^ \"\t\r\n]*"
color body brightgreen black "mailto:[-a-z_0-9.]+@[-a-z_0-9.]+"
mono body bold "(http|ftp|news|telnet|finger)://[^ \"\t\r\n]*"
mono body bold "mailto:[-a-z_0-9.]+@[-a-z_0-9.]+"

# email addresses
color body brightgreen black "[-a-z_0-9.%$]+@[-a-z_0-9.]+\\.[-a-z][-a-z]+"
mono body bold "[-a-z_0-9.%$]+@[-a-z_0-9.]+\\.[-a-z][-a-z]+"

# header
color header green black "^from:"
color header green black "^to:"
color header green black "^cc:"
color header green black "^date:"
color header yellow black "^newsgroups:"
color header yellow black "^reply-to:"
# color header brightcyan black "^subject:"
color header yellow black "^subject:"
color header red black "^x-spam-rule:"
color header green black "^x-mailer:"
color header yellow black "^message-id:"
color header yellow black "^Organization:"
color header yellow black "^Organisation:"
color header yellow black "^User-Agent:"
color header yellow black "^message-id: .*pine"
color header yellow black "^X-Fnord:"
color header yellow black "^X-WebTV-Stationery:"
color header yellow black "^X-Message-Flag:"
color header yellow black "^X-Spam-Status:"
color header yellow black "^X-SpamProbe:"
color header red black "^X-SpamProbe: SPAM"

# Coloring quoted text - coloring the first 7 levels:
color quoted cyan black
color quoted1 yellow black
color quoted2 red black
color quoted3 green black
color quoted4 cyan black
color quoted5 yellow black
color quoted6 red black
color quoted7 green black


# Default color definitions
#color hdrdefault white green
color signature brightmagenta black
color indicator black cyan
color attachment black green
color error red black
color message white black
color search brightwhite magenta
# color status brightyellow blue
color status blue black
color tree brightblue black
color normal white black
color tilde green black
color bold brightyellow black
color underline magenta black
color markers brightcyan black

# Colour definitions when on a mono screen
mono bold bold
mono underline underline
mono indicator reverse
Enter fullscreen mode Exit fullscreen mode

Now set the permission for the directory so that the content in this directory can be copied automatically.

sudo chmod 700 -R /etc/skel/
Enter fullscreen mode Exit fullscreen mode

Step 4 - Creating users

Create two users with names user1 and user2 and set passwords for them.

sudo adduser --gecos "" user1
sudo adduser --gecos "" user2
Enter fullscreen mode Exit fullscreen mode

Image description

Now we will switch to user1 and will view the files in the home directory.

Image description

Image description

Now we will go to this file and we will update the username, password, and real name (signing up) so that the user doesn't have to give the password every time he is sending or receiving mail.

nano .mutt/muttrc
Enter fullscreen mode Exit fullscreen mode

Image description

Now open two terminals - one for user1 and one for user2.

Image description

Run

mutt
Enter fullscreen mode Exit fullscreen mode

it will open the certificate window. Then press "a" to accept the certificate. Do this for user2 also.

Inboxes for both users are open now.

Image description

To send email from user1 to user2:

  • Go to user1 terminal where inbox is open
  • Press "m" to create a new email.
  • Write the email id of user2 "user2@sourav.com" in the To section and press Enter
  • Give any subject, press Enter.
  • Write the email in the editor, press Ctrl + X, then "y" then Enter.
  • Press "y" to send the email.

Image description

  • Go to user2 terminal where inbox is open.
  • Press Enter on the new email received, this will open the email sent by user1.

Image description

And, we have setup the mail server successfully!!

Top comments (0)