Send Emails From Terminal Using Gmail
|Linux terminal is one of the coolest tools I’ve ever come across|
Note: I’ve tested these steps on Ubuntu 12.04 and Fedora 16.
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.
We want to connect to Google securely which means you'll need the latest SSL certificates. To get those, use the openssl client onyour machine. Run:
which should show two blocks ofopenssl s_client -connect imap.gmail.com:993 -showcerts
in the output. You'll want to take each block (including the BEGIN/END CERTIFICATE lines), and put each of them into their own file. I put the first one in a file-----BEGIN CERTIFICATE----- ... -----END CERTIFICATE-----
gmail.crtand the second one in the file
google.crt(since the first signs imap.gmail.com which is signed by Google Internet Authority, the second certificate). The second certificate, the one for the Google Internet Authority, is signed by Equifax. So we'll need Equifax's certificate also. An as it turns out, Ubuntu has a copy of Equifax's certificate already sitting in the repositories. Just run
to get the latest CA certificates. After installing the CAs,Equifax's CA sits atsudo apt-get install ca-certificates
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;
sudo apt-get install msmtp heirloom-mailx // DebianConfiguring msmtp
sudo yum install msmtp mailx // RPM
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
#Gmail SMTP host name
#sets Transport Layer Security 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)
#your email id here (e.g. firstname.lastname@example.org)
#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.
To configure mailx, we need to create another configuration file, ~/.mailrc. The following code goes in that file:
#your email id here
#location of msmtp's binary executable
#additional arguments to msmtp goes here (optional) (-a indicates account name to be used to send mails)
gain 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@example.com). 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:
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 //Debian
sudo yum remove msmtp mailx. //RPM
Hardeep Singh aka cyb3r.gladiat0r