The Complete Magazine on Open Source

Sending Emails From Terminal Using Gmail Account

3.36K 22

Linux terminal is one of the coolest tools I’ve ever come across. If you are also a fan of the terminal, and are a Gmail user, you should like this article.

Note: I’ve tested these steps on Ubuntu 12.04 and Fedora 16.

Security certificate

Before proceeding, we need Gmail’s security certificate on our system. Why? Gmail sends encrypted data over SSL (Secure Sockets Layer) for security; this certificate is necessary for encryption of data.

Ideally, you should have the file Equifax_Secure_CA.crt under /usr/share/ca-certificates/mozilla/. If you do, you’re ready to move on. If you don’t have it, steps 6 & 7 at may help you. Set the tls_trust_file parameter (in msmtp configuration, described below) to the path of the certificate file.


We need to install two packages–msmtp (a simple and easy-to-use SMTP client), and mailx (a utility program to send and receive emails). In a terminal, run the appropriate command for your distro; for systems supporting deb packages (e.g. Debian, Ubuntu, LinuxMint etc.), use sudo apt-get install msmtp heirloom-mailx; for systems supporting RPM packages (e.g. RedHat, Fedora etc.), use sudo yum install msmtp mailx.

Configuring msmtp

We need to create an msmtp configuration file—create a file named .msmtprc in your home directory, and open it in your favourite editor. Copy the following code and paste it in the file.


# Gmail account starts
# account name which must be unique for each account
account gmail1
auth on
#Gmail SMTP host name
port 587
#sets Transport Layer Security on
tls on
# location of tls certificate file for Gmail (change this parameter if your certificate file is stored at some other location in your File-system)
tls_trust_file /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.crt
#your email id here (e.g. [email protected])
#your password here
#email id of the sender that is you again
# Gmail account end
#So we have added one account. Similarly we can add more accounts by repeating and modifying above code for each new account. Make sure you give unique name to each account.
#set default account to be used when no account is specified (Not necessary for single account)
account default: gmail1

Don’t forget to replace the capitalised words with values specific to your account. Save and close the file. To get msmtp to work properly, we need to set proper permissions on this file, with the command chmod 600 ~/.msmtprc in the terminal. This makes the file’s contents viewable by only your account and root.

Configuring mailx

To configure mailx, we need to create another configuration file, ~/.mailrc. The following code goes in that file:

set from="YOUR_EMAIL_ID"                    #your email id here
set sendmail="/usr/bin/msmtp" 		#location of msmtp's binary executable
set message-sendmail-extra-arguments="-a gmail1" #additional arguments to msmtp goes here (optional) (-a indicates account name to be used to send mails)

Again replace capitalised words with values specific to your account. Save and close .mailrc. Now you are all set to send your first mail from the terminal.

Note: The .msmtprc and .mailrc files in the user’s home directory means they are user configuration files. Each user of the system who wants to send emails from the terminal will have to create his/her own configuration files in his home directory. The same files can be created as system configuration files, in the /etc directory, if all users on the system are sharing one Gmail account. I would personally prefer user configuration files, because I want to keep my personal Gmail login information confidential.

Sending your first email

To send your first mail, run mailx RECIPIENT_EMAIL_ID (here, replace RECIPIENT_EMAIL_ID with the actual email address to send your mail (e.g. [email protected]). You will be prompted to enter Subject and Body of the mail. Once you’re done typing the body of the message, press Enter and Ctrl+D to send the mail. If you don’t receive any error message on terminal then your mail is sent successfully.

Useful mailx arguments

Here are some command-line switches to mailx that you may find useful.

-s: Specify a subject (e.g. mailx -s “subject line” RECIPIENT_EMAIL_ID)

-a: Add an attachments (e.g. mailx -a PATH RECIPIENT_EMAIL_ID) (Replace PATH with the full path to the file you want to attach.)

You can explore more arguments in the mailx manual page (run man mailx).

One more trick: you can redirect the contents of a file as the body of the mail with:

mailx -s "subject line" RECIPIENT_EMAIL_ID < /path/body.txt

Rolling back

If you want to undo the above experimentation, delete the configuration files (rm ~/.mailrc ~/.msmtprc) and remove the packages with the appropriate command for your system—for systems supporting DEB packages, sudo apt-get remove msmtp heirloom-mailx and for systems supporting RPM packages,

sudo yum remove msmtp mailx.

  • mehul

    Kind of new.

  • sachin p

    how can I use it behind proxy ??

    • vatsalmevada

      Hi ..sorry for the late reply buddy….I’m not getting your exact question …can you please elaborate a little more ….

      • sachin p

        I mean, I m behind proxy server, so how can I configure to work behind it ?
        I tried, but no success. ‘msmtp’ don’t provide proxy setting

        • sam

          export HTTP_PROXY=http://username:[email protected] : portno
          in .bashrc for global settings if you want to set up it for your profile
          then .bash_profile file

          otherwise find out for setting proxy for terminal on google , not a big issue

          • sachin p

            I tried this earlier, this doest’t work

  • gururaj patil

    why not you releasing the application of mac operating system in magzines.

  • PuNeEt hattalli

    suppose we add two gmail accounts then which account will be used to send mail

    • vatsalmevada

      Have look at the last line of the msmtprc file..It’s “account default: gmail1″…So when you have added two accounts, msmtp will use account specified in default parameter(In this case gmail1)….Note that gmail1 is the name of the first account specified on line 3 by account parameter….”account gmail1″…

  • Vikram Mahendrakar

    Hi, I don’t want to store my password as plain text in a file!. Can I enter password on terminal when I send mail?

  • Afiz

    Good Article

  • deepak

    nope its not working… i am behind proxy exported http_proxy. but still its not working…

    • vatsalmevada

      Hi deepak …Are you getting any error message which might help debug d problem ??

  • udit007

    I am receiving this error message:
    [[email protected] ~]$ send-mail: cannot set X509 trust file /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.crt for TLS session: Error while reading file.
    send-mail: could not send mail (account default from /home/udit/.msmtprc)

    • vatsalmevada

      Hi udit…I think you have problem with permissions of the file…Fire this command, it should fix the problem.:

      “sudo chmod 644 /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.crt”

  • Mr Bill

    Nice! Many thanks. FYI, though, if you send to [email protected] and your ‘user’ is [email protected] then mail goes into the sent folder :-) Looks like you either need to send to non-gmail account or have two gmail accounts. Just a note.

  • Saikat Chatterjee

    Thanks a lot for the commands. I’m being able to send emails via mailx, but it’s somehow not showing any new mail of my inbox.
    Can you detect what’s the problem?

    • vatsalmevada

      mailx command will by default show system mailbox of current user…. So if you want to check your gmail inbox using mailx then fire this command:

      “mailx -f imaps://[email protected]

      replace “username” by your gmail username…

  • indiajoe

    Nice article. I just wanted to add a comment that instead of rm ~/.msmtprc. If people want to remove it they should use “shred” command for the safety of password written in text file.

  • Diego Lopes da Cruz

    Very nice! Works fine here! Thanks from Brazil!

  • can I install this on a VPS? will WordPress and fail2ban be able to use it to send me notification ???

  • Deepen Dhulla

    easy would be using file from example as below

    -o tls=yes -f YourEm[email protected] -t [email protected] -s -xu [email protected] -xp YOURPASSWORD -u “Hello
    from sendEmail” -m “How are you? I’m testing sendEmail from the command