Setting up an email server takes a considerable amount of time and requires intricate knowledge of the many services necessary to download and save email, send email, and provide a user interface for users to interact with their messages. All of these services require knowledge of security best practices. For our example we will be using a prebuilt docker container that has all of these services setup for us.
In this lab you will learn
ist346-labs
PS > cd ist346-labs
PS ist346-labs> $Env:COMPOSE_CONVERT_WINDOWS_PATHS=1
PS ist346-labs> git pull origin master
lab-K
folder:PS ist346-labs> cd lab-K
PS C:\ist346-labs\lab-K> docker-compose up -d
mailserver
email server, containing Postfix (email inbox server) and an SMTP server for outgoing mail, a roundcube
container, which is a webmail client, and a workstation
container so you can learn to send SMTP mail manually.PS C:\ist346-labs\lab-K> docker-compose ps
We can send and receive mail using the https://en.wikipedia.org/wiki/Telnet program. This allows us to interact with the mail server from our terminal at the protocol level. We will Telnet into the SMTP service on mailserver and then issue protocol commands to send an email. It’s a great way to see how the SMTP (simple mail transport protocol) actually goes about sending an email.
PS C:\ist346-labs\lab-K> docker compose exec workstation bash
root@workstation:/# telnet
mailserver
to send an email. This is TCP port 25, the well-known port number for SMTP:
telnet> open mailserver 25
Trying 192.168.144.2...
Connected to mailserver.
Escape character is '^]'.
220-mail.mycompany.com ESMTP Postfix (Debian)
ehlo mycompany.com
250-mail.mycompany.com
250-PIPELINING
250-SIZE 1048576
250-ETRN
250-STARTTLS
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-DSN
250 SMTPUTF8
mail from: me@mycompany.com
250 2.1.0 Ok
rcpt to: myuser@mycompany.com
250 2.1.0 Ok
data
354 End data with <CR><LF>.<CR><LF>
My Test Email from me! .
Don't forget the . at the end!
If everything worked the you should see something similiar to:
`250 2.0.0 Ok: queued as E0684127C`
9. Then quit out of the smtp server:
`quit`
That should have delivered our message to our `myuser` mailbox at our fake company `mycompany.com`.
## Retrieve Mail with IMAP using the Telnet client
IMAP (internet message access protocol) allows for the recieving of mail from a mailbox. SMTP does the sending to a mailbox, and IMAP allows a user to retieve it. In this part we will again use Telnet to connect to IMAP running on our mailserver and read the email we just sent. Again, this is not commonly how one would use email, but it does help you understand how application-layer protocols like SMTP and IMAP work under the covers!
Any real mail client we use whether it be outlook or a web client must implement these SMTP and IMAP commands to actually send and retrieve emails respectively.
1. Now lets check the email using IMAP. Again connect using telnet:
`root@telnet:/# telnet`
2. But this time connect to mailserver on port 143, which is the well-known TCP port for IMAP. `telnet> open mailserver 143`
If it works, you should see a line of text starting with:
`* OK [CAPABILITY...`
3. Once connected you can login a user `myuser` with the pre-set password `mypassword`:
`a login myuser@mycompany.com mypassword`
You should see another `OK` response.
4. Now list the available mailboxes for this account:
`a list "" "*"`
You should see all the mailboxes setup for the *myuser* account:
5. Lets check the inbox!
`a examine inbox`
a FETCH 1 BODY[]
You should see your test email that you sent through the server. Not as pretty as GMail! Haha* 1 FETCH (BODY[] {1393}
Return-Path: <me@mycompany.com>
Delivered-To: myuser@mycompany.com
Received: from mail.mycompany.com
by mail.mycompany.com with LMTP id GD2SE5bY0FtqUAAAScxBiw
for <myuser@mycompany.com>; Wed, 24 Oct 2018 20:39:50 +0000
Received: from localhost (localhost [127.0.0.1])
by mail.mycompany.com (Postfix) with ESMTP id 2BF9D12CC
for <myuser@mycompany.com>; Wed, 24 Oct 2018 20:39:50 +0000 (UTC)
X-Quarantine-ID: <WwSMRRt2wJmJ>
X-Virus-Scanned: Yes
X-Amavis-Alert: BAD HEADER SECTION, Missing required header field: "Date"
X-Spam-Flag: NO
X-Spam-Score: 2.743
X-Spam-Level: **
X-Spam-Status: No, score=2.743 tagged_above=2 required=6.31
tests=[ALL_TRUSTED=-1, MISSING_DATE=1.396, MISSING_FROM=1,
MISSING_HEADERS=1.207, MISSING_MID=0.14]
autolearn=no autolearn_force=no
Received-SPF: None (mailfrom) identity=mailfrom; client-ip=192.168.144.3; helo=mycompany.com; envelope-from=me@mycompany.com; receiver=<UNKNOWN>
Authentication-Results:mail.mycompany.com; dkim=permerror (bad message/signature format)
Received: from mycompany.com (lab-k_telnet_1.lab-k_default [192.168.144.3])
by mail.mycompany.com (Postfix) with ESMTP id E0684127C
for <myuser@mycompany.com>; Wed, 24 Oct 2018 20:38:27 +0000 (UTC)
Subject: Test Email
Message-Id: <20181024203950.2BF9D12CC@mail.mycompany.com>
Date: Wed, 24 Oct 2018 20:39:50 +0000 (UTC)
From: me@mycompany.com
My Test Email
)
a OK Fetch completed (0.001 + 0.000 secs).
a logout
root@telnet:/# exit
Using telnet is no way to send and read your email. Most people use an email client, which is software that takes your actions and formulates the SMTP and IMAP commands for you
For example when you compose a new email and hit send, your email client issues the same SMTP commands we typed into Telnet to your SMTP server on your behalf. This is hidden from us and the email just seems to send… which is a really good thing for us! Same thing is true for listing mail folders and reading messages. The client translates our actions (clicking on a message) into IMAP commands to fetch the message.
The value of Telnet is it helps you understand how the protocol works. You can use telnet to learn other TCP protocols such as HTTP. Telnet can be used to trouble issues with protocols help you to determine the problem is with the client’s implementation of the protocol. Something I’ve done a couple of times in my career.
Now let’s try an actual mail client. I think most of you understand how to use an email client, so instead we will think about how it works under the hood using SMTP and IMAP. The roundcube
container is running a web-based email client on port TCP/8080. We should be able to use it to login and send and receive emails.
a examine inbox
command!That is about the limit of what you can do with our simple mail setup. It should be noted that production ready setups address many other factors suitable for email in production. Just a few issues with this setup:
To tear down this lab:
PS C:\ist346-labs\lab-K> docker-compose up down