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
Showing posts with label SMTP. Show all posts
Showing posts with label SMTP. Show all posts
Friday, January 14, 2011
Friday, October 15, 2010
Configuring Postfix SMTP as your MTA
Postfix is one of the most popuar MTAs around. Postfix has a modular design that improve security,speed and east of configuration. In this design, a master daemon controls individual light weight processes (modules) that performs specific tasks. The master deamon binary is "/usr/sbin/postfix" and the daemon configuration file is /etc/postfix/mster.cf. But this file rarely needs change unless you need to enable / disable some sub processes(modules). The MTA configuration is in /etc/postfix/main.cf. There are other configuration files for specific purposes and all of them lies under the folder /etc/postfix. Following is a minimal configuration of main.cf that will let you run a postfix mail server for your own domain.
Most of the configuration parameters in main.cf will have a commented example version, you can either uncomment and edit them, or add a new line. I will just list out important configuration parameters with comments added. The example configuration is for running an MTA for safeer.in domain.
## The configuration file is in "key = value" format. The key defined previously in the file
## can be referred to as $key in the coming sections.
## The domain for which you are configuring MTA
mydomain = safeer.in
## FQDN of the mail server where postfix is being configured
myhostname = smtp.safeer.in
## The domain name that locally sent and received mail appears to be from and/or delivered to.
## When you are running mail server for a domain, set as follows, default if unset is to use hostname.
myorigin = $mydomain
## By default postfix listens only on loopback, make it listen an all interfaces.
inet_interfaces = all
## The SMTP server will accept mails to the email ids addressed to the following domains and/or hostnames.
## In our setup, this will include the mail server (smtp.safeer.in) and the mail domain safeer.in
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
## Define trusted networks from which clients can connect to the smtp server without any restrictions.
mynetworks = 192.168.4.0/24, 10.10.5.0/24
## Allow smtp clients only from $mytnetworks and reject un authorized clients
smtpd_recipient_restrictions = permit_mynetworks, reject_unauth_destination
This will get you started off with a minimal SMTP server. Make these changes and restart your postfix server.
sudo /usr/sbin/service postfix
Ensure postfix has started listening by running
sudo netstat -atnp|grep :25|grep LISTEN
To test your server is working from command line, visit this link
Test your SMTP server with telnet
To know more about postfix configuration parameters check the official website of postfix or look at the man page by running "man 5 postconf"
Most of the configuration parameters in main.cf will have a commented example version, you can either uncomment and edit them, or add a new line. I will just list out important configuration parameters with comments added. The example configuration is for running an MTA for safeer.in domain.
## The configuration file is in "key = value" format. The key defined previously in the file
## can be referred to as $key in the coming sections.
## The domain for which you are configuring MTA
mydomain = safeer.in
## FQDN of the mail server where postfix is being configured
myhostname = smtp.safeer.in
## The domain name that locally sent and received mail appears to be from and/or delivered to.
## When you are running mail server for a domain, set as follows, default if unset is to use hostname.
myorigin = $mydomain
## By default postfix listens only on loopback, make it listen an all interfaces.
inet_interfaces = all
## The SMTP server will accept mails to the email ids addressed to the following domains and/or hostnames.
## In our setup, this will include the mail server (smtp.safeer.in) and the mail domain safeer.in
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
## Define trusted networks from which clients can connect to the smtp server without any restrictions.
mynetworks = 192.168.4.0/24, 10.10.5.0/24
## Allow smtp clients only from $mytnetworks and reject un authorized clients
smtpd_recipient_restrictions = permit_mynetworks, reject_unauth_destination
This will get you started off with a minimal SMTP server. Make these changes and restart your postfix server.
sudo /usr/sbin/service postfix
Ensure postfix has started listening by running
sudo netstat -atnp|grep :25|grep LISTEN
To test your server is working from command line, visit this link
Test your SMTP server with telnet
To know more about postfix configuration parameters check the official website of postfix or look at the man page by running "man 5 postconf"
Subscribe to:
Posts (Atom)