Table of Contents

How to send an email at the command line with SMTP? Email transaction explained

About

This page is a how-to that describes how you can transport an email to a SMTP server at the command line using the SMTP protocol for further delivery

It will show you the inner mechanisms of SMTP.

Example of SMTP transaction example

Below is an example of SMTP transaction, if you don't understand it. At the end of this page, you will.

220 eraldy.com ESMTP Eraldy
EHLO Foo
250-eraldy.com
250-8BITMIME
250-SIZE 10240
250-STARTTLS
250-CHUNKING
250 Ok
MAIL FROM:<[email protected]> SIZE=328
250 <[email protected]>... Sender Ok
rcpt to:<[email protected]>
250 <[email protected]>... Recipient Ok
DATA
354 Start mail input; end with <CR><LF>.<CR><LF>
Subject: Hey!

We should meet up! ;)
.
250 Ok
QUIT
221 eraldy.com Service closing transmission channel

How does SMTP work?

The SMTP protocol works via a series of SMTP command between the server and the client.

Each command has:

The server will then respond with a reply code and a human text that defines if the command was successful.

This page shows you how to use the protocol and its commands so that you can transmit an email to a SMTP server.

You will act as a client known as a mail user agent (MUA).

Steps

Step 1 - Connection

The first step is to connect to the SMTP server.

Without TLS

The port 25 is the default port for a public server and should be configured without TLS

This port is normally only for receiving an email but if you make a connection with the terminal from localhost, it should be permitted to relay the email to the final SMTP server.

Example with:

set localecho
set logfile c:\TelnetTest.txt
telnet localhost 25

With TLS

With TSL, you use OpenSsl.

The submission port (ie where you can submit an email) is on:

Example:

openssl s_client \
   -connect server.example.com:587 \
   -servername server.example.com \ # the sni
   -crlf
# example with google
openssl s_client -connect smtp.gmail.com:465 -crlf
openssl s_client \
   -connect server.example.com:25 \
   -starttls smtp \ # start tls
   -name hostname \ # the hostname given in the EHLO
   -servername server.example.com \ # the sni
   -crlf

where:

Step 2 - Commands and Replies

Server: Banner

After a successful connection, the SMTP server responds with a banner

220 smtp.eraldy.com ESMTP Postfix

Client: EHLO

The remote SMTP client may introduce itself by giving its fully qualified domain name with the EHLO command (Extended HELO)

EHLO client.example.com

Server: Configuration

The SMTP server answers by advertising its configuration.

250-server.example.com
250-8BITMIME
250-PIPELINING
250-SIZE 10240000
250-AUTH DIGEST-MD5 PLAIN CRAM-MD5
250-CHUNKING
250-ENHANCEDSTATUSCODES
250 SMTPUTF8
...

where:

There are other extensions but they are the common ones.

Envelope Creation

When you transport a message with SMTP, you create an envelope where

Steps:

Define the sender

Define the sender address (it can be the same than the FROM address header of the email message but it can also be another one)

MAIL FROM:<[email protected]>
250 <[email protected]>... Sender Ok
Define the recipients

Enter the recipient (one recipient by line).

RCPT TO: <[email protected]> NOTIFY=success,failure
250 <[email protected]>... Recipient Ok

Note the recipient may be not the TO of the email message. For instance, in the case of a mailing list or group.

Example:

from:	  [email protected]
reply-to: [email protected]
to:	  Abridged recipients <[email protected]>

Pass the message

You give the data with the DATA command. After a successful response from the server, you can copy and paste the message.

Steps:

DATA
354 Start mail input; end with <CRLF>.<CRLF>
Subject: Hey!

We should meet up! ;)
.
250 Ok

End

QUIT

If you want to exit, you use the QUIT command

QUIT

The server should reply with a 221

221 eraldy.com Service closing transmission channel
Transmitting another message

You can also transmit another one by resetting the state with the RSET command and starting all over again without quitting

RSET

End

That's it. You have successfully transported an email to an SMTP server and you have improved your knowledge value. Felicitations.