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 "firstname.lastname@example.org" << EOF
> subject: Test mail
> from: email@example.com
> Hello how are you
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
Connected to localhost.
Escape character is '^]'.
220 lintel.localdomain ESMTP Postfix
mail from: firstname.lastname@example.org
250 2.1.0 Ok
rcpt to: email@example.com
250 2.1.5 Ok
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.
250 2.0.0 Ok: queued as BC706A1E7B
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
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.