GrabDuck

sendEmail: send Gmail emails from terminal – Tuxdiary

:

gmail_compThere is an easy way to send git patches via gmail’s SMTP server using git send-email. A similar easy to set up cmdline utility to send mails from the terminal is sendEmail. There are other options (or sendmailmutt) but they are bulkier or have a steep learning curve for the casual user.

In this article we will explore how to send emails and attachments (using your gmail account) with sendEmail. Though it’s a very smart utility with minimum dependencies written in perl, using sendEmail is not so easy because of the overwhelming number of options and somewhat inconvenient shortcuts. A few steps to greatly improve the experience follow.

Features

  • Quickest way to send mails via gmail
  • Option to use TLS
  • Add CC, BCC
  • Add multiple attachments
  • Edit message body in standard input (STDIN) for long messages
  • Minimal dependencies
  • Works with Linux, Windows and Mac

Installation

To install sendEmail from Ubuntu, run:

$ sudo apt-get install libio-socket-ssl-perl libnet-ssleay-perl sendemail

Usage

sendEmail has many options. Have a look:

$ sendemail --help

A sample mail from the cmdline with 2 attachments:

$  sendemail -f myusername@gmail.com -t recipient@domain.com -u "Subject line goes here" -s smtp.gmail.com:587 -o tls=yes -xu myusername -a attachment1.pdf attachment2.mp3
Password: mypassword
Reading message body from STDIN because the '-m' option was not used.
If you are manually typing in a message:
- First line must be received within 60 seconds.
- End manual input with a CTRL-D on its own line.

Hi!

Just a test mail with attachments.

Regards,

User
Sep 09 19:30:59 localhost sendemail[3297]: Message input complete.
Sep 09 19:31:04 localhost sendemail[3297]: Email was sent successfully!

We used STDIN for writing the message in this example. In case of a short message, the -m option is more convenient. Adding attachment is optional, used for a more complete example.

Improvements

  1. As you can see, remembering (and typing) so many options is a pain. On a personal laptop you can set an alias in ~/.bashrc for the constant fields:
    alias mail='sendemail -f myusername@gmail.com -s smtp.gmail.com:587 -o tls=yes -xu myusername -t '

    Note the -t at the end to avoid typing the token each time. No problem even if you include -t. Only -cc (or -bcc) works as well.

  2. sendEmail doesn’t care if you miss the subject. In real life, we seldom send mails without a subject. At the same time, you may not want to remember the -u option.
  3. sendEmail shows the password and it’s a security concern (and don’t even think of using the -xp option).

Download this small patch I wrote to prompt for a subject (if -u is missed) and turn password echo off. If you intend to use an empty subject, simply hit <Enter>. The patch is for version 1.56-5 available on Ubuntu 14.04 at the time of writing. To apply the patch on the original sendEmail file, run:

$ sudo patch /usr/bin/sendEmail < sendEmail-1.56-5.patch

In case you have a newer version and applying the patch fails, check the patch details and make the changes manually. It’s not a complex modification.

Here’s the same example with the alias set and the patch applied:

$ mail recipient@domain.com -a attachment1.pdf attachment2.mp3
Subject: Subject line goes here
Password: Reading message body from STDIN because the '-m' option was not used.
If you are manually typing in a message:
  - First line must be received within 60 seconds.
  - End manual input with a CTRL-D on its own line.

Hi!

Just a test mail with attachments.

Regards,

User
Sep 09 20:48:38 localhost sendemail[6247]: Message input complete.
Sep 09 20:48:49 localhost sendemail[6247]: Email was sent successfully!

Coloured success message

While using sendEmail we noticed that in some cases it is difficult to figure out an error because of the long messages. Here’s a second patch (on top of the previous one) to print the success message in green. Use it on a colour-aware display and a terminal that understands colour (most of them do nowadays).

Here’s the simplest use-case with this modification:

$ mail recipient@domain.com
Subject: Subject line goes here
Password: Reading message body from STDIN because the '-m' option was not used.
If you are manually typing in a message:
  - First line must be received within 60 seconds.
  - End manual input with a CTRL-D on its own line.

Hi,

Just a mail without attachments.

Regards,

User
Sep 09 20:59:08 localhost sendemail[30183]: Message input complete.
Sep 09 20:59:12 localhost sendemail[30183]: Email was sent successfully!