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.

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 [IPv6:::1]
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
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:

  • telnet
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:

  • 25 with plain text connection and StartTLS
  • 465 with SSL
  • and nowadays on 587 with TLS

Example:

  • On port 465, 587
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
  • On port 25 with STARTTLS
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:

  • 250 means all good
  • 8BITMIME means that you can send the data in 8BIT.
  • PIPELINING means that you can group the MAIL, RCPT commands together
  • SIZE indicates the maximum size of a message.
  • AUTH indicates the type of authentication supported (ie the type of SASL mechanisms)
  • CHUNKING means that you can send the message not line by line but in chunks of bytes with the BDAT command
  • ENHANCEDSTATUSCODES means that the reply will contain an extra status code in the text (known as rfc2034 - Returning Enhanced Error Codes)
  • STARTTLS means that you can make use STARTTLS to go from a plain connection to a secure SSL/TLS connection.
  • 'SMTPUTF8 means that the server is international and supports UTF8 as character encoding for email address, headers and status.

There are other extensions but they are the common ones.

Envelope Creation

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

  • the MAIL command define the sender address
  • the RCPT command define the recipients

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]>
  • The reply should look like that
250 <[email protected]>... Sender Ok
Define the recipients

Enter the recipient (one recipient by line).

RCPT TO: <[email protected]> NOTIFY=success,failure
  • The reply should look like that
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]>
  • at reception, the SMTP server will add the Delivered-To headers.

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:

  • Enter the data command
DATA
  • The reply should look like that
354 Start mail input; end with <CRLF>.<CRLF>
  • Copy, paste the message and enter. Note that the message is overly simplified. In the real world, with an SMTP client, it would have more headers (such as content type, date, …). The server should apply the defaults (ie ASCII encoding, send date as now, …)
Subject: Hey!

We should meet up! ;)
  • Terminate with a single point and enter
.
  • If the email was accepted, a 250 reply should be received.
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.





Discover More
Card Puncher Data Processing
Email (Internet Message)

An Email is a document known as the MimeMessage or Internet Message Format that: is send and received between email server and managed via a client known as the Mail User Agent This is in the marketing...
Email - Mille and one way to send an email (professional and user)

This article shows you Mille and one way to send an email (ie known as transport in SMTP. submission port Before continuing, you need to know the basics of email and how it works with SMTP. If you...
Openssl

openssl is a command line tool that: * permits testing, managing, and seeing what happens in a TLS world (ie new name for SSL) * can act as a network client/server * Obtain a valid Certificate...
The SMTP Protocol

A summary of what the SMTP protocol is and how to send an email from a network connection with telnet or netcat
What is a Mail eXchange or Email mail delivery agent (MDA | LDA)?

A mail delivery agent (MDA) or Mail eXchange is a SMTP server that is responsible for the local delivery (reception) of e-mail messages to a mailbox It's also known as: message delivery agent (MDA)...
What is a Milter (mail filter)?

A milter or mail filter is a filter extension for SMTP server (mail transfer agents - MTA) that transforms a email message It refers to a protocol and API for communicating mail traffic information...
What is an Email Address?

An email address is a Internet identifier for: in a SMTP transaction an email sender a recipient (inbox) in a email message a to header address a from header address a cc header address...
What is an SMTP envelope?

SMTP borrows the postal term envelope, ie the email message is put in an envelope that nobody can see. The envelope is defined during a SMTP transaction by the commands: MAIL: that define the sender...
What is an SMTP reception?

SMTP has two sides, sending and receiving, this article is all about how to receive
What is the Email sender? (MAIL FROM, Envelop Sender, Bounce Address)

The sender is an envelope email address (mailbox) that: indicates where the message came from receives problems delivering the message (Undeliverable message notices/bounce) is set during a SMTP...



Share this page:
Follow us:
Task Runner