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

Watch Free Movies

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
  • [email protected] : 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 [email protected]
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
  • [email protected] : 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 [email protected]
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 protected]

$ echo "Hello geeks" | mail [email protected]

For attaching files in the mails sent by terminal, use this syntax: echo “message” | mail -a /path/to/file [email protected]:

$ echo "Hello geeks, this is the LinuxG PDF Guide" | mail -a ~/linuxg.pdf [email protected]

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 protected]

Examples:

$ echo "alias mike [email protected]" >> ~/.mailrc
$ echo "alias car [email protected]" >> ~/.mailrc
$ echo "alias red [email protected]" >> ~/.mailrc

How to use the mail aliases:

Send “hello mike” to mike ([email protected]):

$ 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 [email protected]

Scroll to Top