Send Gmail from the Linux Command Line

October 6, 2012 by
Filed under: command line, HowTo, linux, Linux Mint, Ubuntu 

In this post I'm going to show how to send an email from the Linux command line through your Gmail account. This can be handy if you're a command line junkie. The real power, however, is in being able to send an email automatically from a script. I'll be doing another post where I use this in a script to notify me when something has changed on my system. For now though, I'll just show the setup so you can send an email from the command line.  This process has been tested on Ubuntu 12.04 and Linux Mint 13.

First off, I must acknowledge that I did not figure this out on my own. Ubuntu Forums member, Stephen Morgan, showed how to do it here.

Install msmtp

The first step is to install the msmtp-mta package.

sudo apt-get install msmtp-mta

After the install is complete you'll need to set up the defaults file with your Gmail account information. You need to create a file in your home directory called .msmtprc.

Affiliate Link
nano ~/.msmtprc

Paste the following into the file and edit the portions in bold to reflect your account information.


#Gmail account
defaults
logfile ~/msmtp.log

account gmail
auth on
host smtp.gmail.com
from your_address@gmail.com
auth on
tls on
tls_trust_file /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.crt
user your_address@gmail.com
password your_gmail_password
port 587

account default : gmail

Save the file and exit the text editor. Since this file contains your account credentials, you'll want to change the permissions to make the file readable only by you.

chmod 600 .msmtprc

Install mailx

Now that your computer is configured to talk to Gmail, you need a command line email program to handle writing your email. For this I'm going to use mailx from the heirloom-mailx package.

sudo apt-get install heirloom-mailx

Now you need to set up the defaults file so that mailx uses msmtp to send out the email. This file is called .mailrc.

nano ~/.mailrc

Now paste the following into the file and save it.

set sendmail="/usr/bin/msmtp"
set message-sendmail-extra-arguments="-a gmail"

You should now be able to send email from your terminal command line.

Sending email from the command line

Now you can send email from the command line like this:

mail -s "Subject" [email protected]

The cursor will go to a blank line. Enter your email message. When you're done, hit <Enter> to go to a blank line and then hit <Ctrl>+D to end your message. You have just sent your email.

Here you can see that I've sent an email to myself from my Gmail account.

Gmail screenshot

You can also use a message saved in a text file rather than entering it interactively. This is especially useful if you're automating this process in a script. In this example, the email is saved in a file called message.txt.

mail -s "Subject" [email protected] < message.txt

This content originally appeared at http://tuxtweaks.com/2012/10/send-gmail-from-the-linux-command-line/.

Comments

16 Responses to “Send Gmail from the Linux Command Line”

  1. Ani Papyan says:

    Thanks a lot! it really helps

  2. jason says:

    I use fedora13, and I get the following error when trying to send and email with mail command as you showed above.

    errormsg='cannot set X509 trust file /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.crt for T
    LS session: Error while reading file.' exitcode=EX_NOINPUT

    I see that I do not have the Equifax_Secure_CA.crt file. How can I fix this problem.

    Thank you.

  3. bk322 says:

    Thanks, I've profited from this.

  4. Luke Mackenzie says:

    I followed the insructions but get:

    send-mail: account default not found: no configuration file available

  5. Tom says:

    nice, simple instructions. works a treat!
    is there any way to attach files?

  6. Edward G Prentice says:

    To use with Gmail's two-step authentication, generate a password for msmtp and use that instead of your regular password in the .msmtprc file.

    http://support.google.com/accounts/bin/answer.py?hl=en&answer=185833

  7. Mike says:

    Exactly what I was looking for and it works! Thanks!

  8. James says:

    Perfect, and super-easy to set up (Ubuntu Precise here). Thanks!

  9. unni smohan says:

    hi thanks this really worked.
    I need to know whether we can do it in redhat linux enterprise editon 5 also?
    if yes then how?

  10. Menno says:

    Does this work if you normally use two-factor authentication for your gmail account?

    • Ton van Overbeek says:

      Yes, works with two-factor authentication.
      You will have to generate an application specific password for msmtp on your pi on your Google account page.
      Use this application specific password in your .msmtprc file.

Leave a Reply

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

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