SMTP ( Simple Mail Transfer Protocol ) is the protocol email servers use to send and receive mails. Normally we do use email clients like Thunderbird,Outlook or the web-mail to send and receive mails. But what happens behind the scene is an exchange of SMTP commands and responses between the client and the mail server. Knowing how to use these commands directly will help you in testing a mail servers functionality. Here we will examine the basic commands and use them to connect to a mail server and send mail.
We will use the telnet program to establish a connection to the mail server ( on port 25 - this can be different based on your mail server configuration )
safeer@penguinpower:~$ telnet smtp.safeer.in 25
Trying 10.25.0.1...
Connected to smtp.safeer.in.
Escape character is '^]'.
220 smtp.safeer.in ESMTP Postfix (Ubuntu)
The first three lines of the output is common for any telnet session, it just says the connection is successfully established.
The fourth line is a response from the mail server. Every response from a mail server starts with an SMTP response code followed by further information. Now in the above line the mail server is displaying its banner which mentions the hostname of the mail server ( smtp.safeer.in ) , the mail server software ( Postfix ) and the operating system on which the server is running ( Ubuntu ). This banner is configurable in your mail server.
In order to communicate with the mail server, you should send SMTP request commands which will be in the format of a command followed by arguments.
1) Once you establish a connection with the mail server, to initiate a conversation with it you would greet the server with the command HELO followed by your hostname. The server will greet you back with response code 250 followed by its hostname.
HELO penguinpower
250 smtp.safeer.in
Now let us compose a mail
2) The first step is to specify the "from" email address. The command is "MAIL FROM: <your email id>". The response code will be 250 followed by the message OK
MAIL FROM: safeer@safeer.in
250 2.1.0 Ok
3) Next specify the recipient address using the command "RCPT TO: <recipient email id>". The response again will be code 250 followed by message.
RCPT TO: praveen@gmail.com
250 2.1.5 Ok
This can be repeated multiple times to allow multiple recipients.
4) Once you are ready to type in the message, enter the command "DATA". Response will be code 354 followed by message.
DATA
354 End data with <CR><LF>.<CR><LF>
The email body will contain your email message and optionally email headers (which should be added at the beginning of the message). A few commonly used message headers are Subject,From,To,Cc
A message header should be of the following syntax:
<HEADER NAME>: <HEADER VALUE>
After the headers, add your message then put a period ( . ) on a new line and press enter to finish the message.
Let us compose our message now.
Subject: Happy New Year!!!
From: safeer@safeer.in
To: praveen@gmail.com
Happy New Year
.
250 2.0.0 Ok: queued as C53E7265CB0
The response from mail server says the message is accepted for delivery.
Remember that the delivery of the message is not dependant on the From/To/Cc headers within the message body. MAIL FROM & RCPT TO parameters decides the delivery. The headers in message is as such delivered to the recipients, and it is the duty of the mail client of the recipient to interpret and present the message headers to the user.
5) If after entering RCPT TO or MAIL FROM you feel that you provided wrong information and want to start over without terminating the existing connection, issue the command RSET ( stands for reset ).
4) We are done. Say goodbye to server.
QUIT
221 2.0.0 Bye
Connection closed by foreign host.
Second line above is the response from server while the third line is the output of telnet command terminating.
For more information on smtp protocol, refer to SMTP RFC at IETF
No comments:
Post a Comment