
A lot of times when working on headless Linux distros, we do not have any email clients with UI. For validating email servers during that time you have to rely on available console programs. One of the age old tricks is to use Telnet. Since email protocol is very simple, it is very easy to send/ receive emails manually.
This is something that every seasoned programmer would have done, but I wanted to put here for easy accessibility. It also helps me to write the commands down somewhere as sometimes I give the commands out of order.
I will assume the following configuration for the email server. The thing to understand here is knowing what the mail server endpoint is and login credentials. For convenience we will be working with insecure server port.
Username: test-user@mydomain.com
Password: Password1
Incoming Server: xchng.mailserver.com
IMAP Port: 143 POP3 Port: 110
Outbound Server: xchng.mailserver.com
SMTP Port: 587
All protocols require authentication.
Email Protocols
Before we proceed further, it makes more sense to briefly go over each protocol definitions.
For inbound messages, traditionally we used POP3 protocol. POP or Post Office Protocol is the older protocol. It was designed to be used by one user and provides the ability to download messages from the server.
IMAP or Internet Message Access Protocol, added additional ability to have multiple device synchronization. So, you can be logged in to different machines accessing the emails and the state of the emails will always reflect the correct status.
SMTP or Simple Mail Transfer Protocol is used for relaying messages to other servers. This is the protocol normally used by email clients to send emails.
RFCs
----
SMTP: https://datatracker.ietf.org/doc/html/rfc5321
POP3: https://datatracker.ietf.org/doc/html/rfc1939
IMAP: https://datatracker.ietf.org/doc/html/rfc3501
Receiving Messages
We will be using POP3 protocol to get messages from the server. This is the easier to understand protocol. Before we start, make sure that TELNET is installed on your machine. If it is not installed, please install it.
linux:~$ which telnet /usr/bin/telnet
Next I will provide a sequence of commands used for getting messages from the server.
linux:~$ telnet xchng.mailserver.com 110 Trying 111.222.333.444... Connected to xchng.mailserver.com. Escape character is '^]'. +OK Mailserver ready. user test-user@mydomain.com +OK pass Password1 +OK Logged in. stat +OK 5 111618 list +OK 5 messages: 1 79370 2 3636 3 5438 4 11600 5 11574 . retr 2 +OK 3636 octets --000000000000b33c8765d2ade621 Content-Type: text/plain; charset="UTF-8" test --000000000000b33c8765d2ade621-- . # dele 2 quit +OK Logging out. Connection closed by foreign host.
This shows a full session with Telnet. Now let’s try to understand what each command does. We initiate the conversation by login in to the server (Line 1). Next we send the user and password for authorization. In this case we have sent plain text user and password (Lines 6 and 8). We then list the messages available on the server (Line 12). Finally we retrieve messages to read (Line 20). When reading the message, we get a lot of routing information. For brevity, I have removed all those text.
Commands
- user <username>: Send the user
- pass <password>: Send the associated password
- stat: List count and total bytes for all messages
- list: Lists all available message IDs with a number and associated bytes
- retr <message_number>: Read the message
- dele <message_number>: Deletes a message
- quit: Exit
Sending Messages
We will use SMTP to send messages. Refer back to the same server list we have above. I will put a SMTP session below. However, before we start we will get the Base64 values for the username and password defined above. We need to send Base64 encoded values for SMTP.
test-user@mydomain.com dGVzdC11c2VyQG15ZG9tYWluLmNvbQ== Password1 UGFzc3dvcmQx
linux:~# telnet xchng.mailserver.com 587 Trying 111.222.333.444... Connected to xchng.mailserver.com. Escape character is '^]'. 220-mailserver-ESMTP-ready ehlo me@mydomain.com 250-mailserver.com Hello me@mydomain.com [444.333.222.111] 250-SIZE 1048576000 250-AUTH PLAIN LOGIN 250-STARTTLS auth login 334 VXNlcm5hbWU6 dGVzdC11c2VyQG15ZG9tYWluLmNvbQ== 334 UGFzc3dvcmQ6 UGFzc3dvcmQx 235 Authentication succeeded mail from: me@mydomain.com 250 OK rcpt to: them@theirdomain.com 250 Accepted data 354 Enter message, ending with "." on a line by itself from: me@mydomain.com to: them@theirdomain.com Subject: Test Email Hello... . 250 OK id=1mxsx3-001oWA-Aj quit 221 mailserver.com closing connection Connection closed by foreign host.
Here we start by login in to the SMTP server. We send ehlo to initiate conversation (Line 6). Mail server sends back some basic information on configuration. Couple of important configurations are type of Auth and TLS setup. Now we tell SMTP that we want to use login for Authorization (Line 11). Server will then prompt for username and password (Line 12 and 14). We provide corresponding user and password encoded in Base64 (Line 13 and 15). Finally we are ready to send the message. We start the message with a data block and end with a . (dot).
Commands
- ehlo <username>: Send a name to the server
- auth <plain | login | cram-md5>: auth modes requested
- mail from: Sender email
- rcpt to: Recipient
- data: Start email body
- from: Within data body, sender email (display)
- to: Within data body, recipient email (display)
- Subject: Within data body, subject of the email
- quit: Exit
Conclusion
As shown above it is not too difficult to use Telnet for emails. This is an especially useful tool on Linux servers where emails clients are not normally installed. Hope you found this useful. Ciao for now!