How to Send Emails Via Terminal

In this article I will show you how to make the propper configurations to be able to send emails from the command line interface. Yes, if you follow my instructions right, you will be able to email your friends on gmail or yahoo mail by the terminal.
The tools you need for emailing by terminal are msmtp and heirloom-mailx. The two packages are in the default repositories for all the popular distros. The Ubuntu and Debian derivates have installed by default the heirloom-mailx package, so you have to install only msmtp on your Ubuntu / Linux Mint / Debian / Knoppix.

Make the installations needed:

$ sudo apt-get install msmtp

Now create a file named .msmtprc in your home directory (~/.msmtprc) and paste the following lines. This settings are available for sending emails by a gmail account.

Replace:

  • Linux Geekster : with the name you want the guy getting the mail to see
  • username@gmail.com : your valid gmail, from which the mail will be sent
  • yourpassword : the password for your valid gmail

host smtp.gmail.com
port 587
protocol smtp
auth on
from Linux Geekter
user username@gmail.com
password yourpassword
tls on
tls_nocertcheck

This are the setting you have to make for sending emails by a yahoo account, paste this in your ~/.msmtprc.

Replace:

  • Linux Geekster : with the name you want the guy getting the mail to see
  • username@gmail.com : your valid yahoo mail, from which the mail will be sent
  • yourpassword : the password for your valid yahoo mail

host smtp.mail.yahoo.com
port 465
protocol smtp
auth on
from Linux Geekter
user username@yahoo.com
password yourpassword
tls on
tls_nocertcheck

Next, write “set sendmail=/usr/bin/msmtp” (with no quotes ” “) in the ~/.mailrc file:

$ echo "set sendmail=/usr/bin/msmtp" > ~/.mailrc

Now set read-write permissions for the user and cancel all the permissions for the group and others (600) to the ~/.msmtprc file:

$ chmod 600 .msmtprc

How to send emails from the terminal:

This is how to send emails from terminal: echo “message” | mail email@address.com”

$ echo "Hello geeks" | mail contact@linuxg.net

For attaching files in the mails sent by terminal, use this syntax: echo “message” | mail -a /path/to/file email@adress.com:

$ echo "Hello geeks, this is the LinuxG PDF Guide" | mail -a ~/linuxg.pdf contact@linuxg.net

Create mail aliases for a easier mail sending:

An alias is a shorter name for an email address. The aliases must be put in the ~/.mailrc file.

Alias syntax: alias alias_name email@address.com

Examples:

$ echo "alias mike mike@gmail.com" >> ~/.mailrc
$ echo "alias car mercedes@yahoo.com" >> ~/.mailrc
$ echo "alias red colours@hotmail.com" >> ~/.mailrc

How to use the mail aliases:

Send “hello mike” to mike (mike@gmail.com):

$ echo "hello mike" | mail mike

Bonus Trick: Write the content of a file in the mail (like: opening text file, select all and copy the text, paste it in your mail box) :

$ cat mail.txt | tee mail make
$ cat mail2.txt | tee mail michael@gmail.com

Tagged with: , , , ,
Posted in The Linux and Unix Articles!
2 comments on “How to Send Emails Via Terminal
  1. Peter says:

    Whiles I can see that msmtp could be useful with bsd-mailx (the default mailx in many distributions (e.g. Debian, Ubuntu)) why use msmtp with heirloom-mailx when heirloom-mailx has smtp capabilities builtin?

  2. Aaron says:

    Maybe I’m missing something here, why was libgsasl7 and libntlm0 also installed?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!