Home » Linux

Send Mail From the Command Line

14 February 2011 No Comment

Send Mail From the Command LineIf you use a Linux system, you probably already know about the Terminal and the power provided to you from the command line. If you use the command line, did you know that you can use the command line to send email?

Sure, you can use such clients as Gmail, Yahoo Mail or Hotmail to send and receive email but what if you want to send email for a cron job or send a quick email to test your email client? How would you do it?

Use sendmail to send email from the command line. On many Linux distributions such as Ubuntu, you can install sendmail by using the apt-get command. To run sendmail, type

$ /usr/bin/sendmail user@domain.com
enter body of message

From the command line, run the sendmail, provide the recipient email address, and press <enter>. Afterwards, enter the body of your message. End the message by pressing the Ctrl and D keys.

The above message works but it doesn”t include the Subject line. What if you also want to change the From line? For example, create a text file called mail.txt with the following information:

to: user@domain.com
from: your-name@domain.com
subject: your-subject

Body of message goes here

To send the message, run the command below:

$ /usr/sbin/sendmail user@domain.com < mail.txt

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.