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:
- a name
- optional arguments
- and terminate with the windows end of line (ie CRLF)
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:
- -starttls smtp will start starttls connection for smtp. ie it:
- will open the connection,
- receive the banner from the server
- sends the EHLO command with the hostname with the value of the name option,
- sends the STARTTLS SMTP command
- starts the handshake.
- -crlf will convert the LF end of line character from the terminal into the CRLF end of line (this is the end command separator for SMTP)
- -servername is the server name indicator (sni) mandatory in case of SMTP server hosting
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:
- the recipient: [email protected]
- the message
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>
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.