Tag Archives: telnet

How to send email command line in linux

We all know that email plays a vital role in our communication system. We will see how to send mail from command line in this article.

Assuming that you have postfix instaled on your machine. You can send the mail from command line by connecting to your local mail server. You will get the utility sendmail with postfix.

send email using sendmail

$ sendmail "blog@lintel.in" << EOF
> subject: Test mail 
> from: user@lintel.in
> Hello how are you 
> EOF

Send email using telnet

We all now about telnet command. It is very helpful to debug and understand the protocols. Here is the simple mail sent using telnet

[centos@lintel ~]$ telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 lintel.localdomain ESMTP Postfix
mail from: naveen@lintel.in
250 2.1.0 Ok
rcpt to: user_name@gmail.com
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
Subject: hello how are you
If you are free please try to call me. 
We will work together.
Than you
.
250 2.0.0 Ok: queued as BC706A1E7B
quit
221 2.0.0 Bye
Connection closed by foreign host.

In the above example we connected to the SMTP server as a client which is running on localhost. We gave commands to the server to send a mail. Please note that  the data ends with   <CR><LF>.<CR><LF> . to indicated it is the end of data. As you can see  . (dot) we mentioned  at the end.

Once we connected, we pass the set commands to server to write and send a mail. Here are the few SMTP commands

SMTP Commands

  •  HELO
  • MAIL
  • RCPT
  • DATA
  • RSET
  • VRFY
  • EXPN
  • HELP
  • NOOP
  • QUIT

We used MAIL, RCPT and QUIT commands in the above example. To understand these commands go through rfc . You may not need to understand low level protocol concepts of SMTP unless if you are really interested or you are working with SMTP.

References:

[1] http://tools.ietf.org/html/rfc5321