Sending an Email Using Telnet

Content:

Whether you’re just setting up your server, or testing your configuration, Telnet is a great tool to send emails through a terminal.

This might be installed already, but if not, install it using

apt install telnet

or equivalent for your distro.

You should already have Postfix, or another MTA, set up and configured to send emails.

Opening a Connection

The first step is to open a telnet connection. The connection will be opened on port 25, on the local machine.

telnet localhost 25

You should see a response similar to the below.

Connected to localhost.
Escape character is '^]'.
220 server ESMTP Postfix (Debian)

The server name and distro name on the last line will differ depending on the hostname and OS of your server.

Next, send the ehlo command. This sends an ‘extended hello’ to the Postfix server.

ehlo localhost

This connects to the server, and informs it that our client supports ESMTP. ESMTP is the name of the protocol used for email transmission.

250-server
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-DSN
250-SMTPUTF8
250 CHUNKING

The response will include a list of features supported by the server. Make sure the server responds with lines starting with status code 250.

Writing the Email

There are a few parts to writing the email.

Start with the mail from entry. This is the email address to send the email form. You can use either the full email (including the domain), or just the system username.

mail from: postmaster@example.com

Next, enter the rcpt to address. This is the email address of the intended recipient.

rcpt to: user@example.com

These commands have set the relevant email headers. Now comes the content of the email. This is the user-visible data, displayed by the receiving server.

Start by entering data, which defines the start of the message content.

data

Data following this will be included in the email message.

It’s a good idea to add the to field inside the message content, to please spam filters.

to: user@example.com

Follow this with the subject.

subject: Test Message

Now enter the message you want to send. Keep it simple for test purposes.

Once you’ve completed the message, add a new line containing a single ., as below.

This is a test message
.

Once this is done, you’ll see a message stating that the message is queued, with an ID number.

250 2.0.0 Ok: queued as 89FE01FF6B

The full list of commands entered are below.

mail from: postmaster@example.com
rcpt to: user@example.com
data
to: user@example.com
subject: Test Message
This is a test message
.

You’ll receive responses after each part entered. The an example of a complete terminal session, including user inputs, is below.

Connected to localhost.
Escape character is '^]'.
220 server ESMTP Postfix (Debian)
ehlo localhost
250-server
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-DSN
250-SMTPUTF8
250 CHUNKING
mail from: postmaster@example.com
250 2.1.0 Ok
rcpt to: user@example.com
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
to: user@example.com
subject: test
test email
.
250 2.0.0 Ok: queued as 89FE01FF6B

To exit telnet, type

quit

You can now check whether the email has been sent successfully by your MTA.

Checking the Email has been Sent

You can check that the message has been sent using the mail.log file.

cat /var/log/mail.log

You should see messages that include the ID number given in telnet. Specifically, you’re looking for one that looks similar to this.

89FE01FF6B: to=<user@example.com>, orig_to=<user@example.com>, relay=local, delay=17, delays=17/0.11/0/0.03, dsn=2.0.0, status=sent (delivered to mailbox)

The important part of this is right at the end – the email has been sent, and delivered to the mailbox.