Latest in Gcc

Image credit:

Use mailsend to send email from the Terminal

TJ Luoma , @tjluoma

Update: After this article was written I learned of mstmp which I highly recommend instead of mailsend.

There are times that I want my iMac to be able to email me: when certain scripts run via cron or launchd, when certain events happen (a backup has been completed), etc. I've found that none of the included command-line programs work.

The good news is that, with a UNIX foundation, it was fairly easy to find a free program which would do just that.

A little searching turned up a free program called mailsend, which will work. For this example I will be using a Gmail account, which requires OpenSSL. Your mail server may not require OpenSSL support, but if it's possible, I encourage you to use it.

The short version of the instructions are as follows:

1) Download and install OpenSSL to /usr/local/ssl/

2) Download and install mailsend to somewhere in your path such as /usr/local/bin/

3) Use mailsend -h to learn how to use it on the commandline

4) (Optional) Use TextExpander 3 to fill in some of the variable fields, such as To, Subject, and Message.

Read on for more of a step by step walk-through.

Step 1) Download and install OpenSSL

Note: You should check for the most recent version of OpenSSL, which is 1.0.0 as of this writing.

Also note that these commands in bold should be entered in /Applications/Utilities/ If you're not comfortable with Terminal, this probably isn't for you.

Download the source:
curl -O

Unarchive it: tar zxvf openssl-1.0.0.tar.gz

Chdir into the folder, configure it and run 'make': cd openssl-1.0.0 && ./config –openssldir=/usr/local/ssl && make

Note: If this fails, check "gcc –version" to make sure you have a recent version. When I tried to compile using "i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5646) (dot 1), " it failed. When I compiled using "i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5659)," it worked fine. You can download gcc as part of Xcode if you don't already have it installed.

Install OpenSSL: sudo make install

(You will be asked to enter your administrator password. Note that it will not show up when you type.)

Step 2) Download and install mailsend

Be sure to check the mailsend Web site for the latest source. You want the version with STARTTLS and Authentication support. As of today the most current version is mailsend1.15b5.tar.gz

Download the source: curl -O

Unarchive it: tar zxvf mailsend1.15b5.tar.gz

Chdir to the new folder: cd mailsend1.15b5/

Configure mailsend with OpenSSL support: ./configure –with-openssl=/usr/local/ssl && make

When it's finished, you should have a "mailsend" binary in the current folder.

Verify the version number and OpenSSL support: ./mailsend -V

You should show something like this:

mailsend Version: @(#) mailsend v1.15b5
Compiled with OpenSSL 0.9.8l 5 Nov 2009

Now all that's left is actually using it. Here's how to send a simple test message to your friend ( from your email account (, using your Gmail password (SuPerSEKret):

./mailsend -smtp \
-port 587 \
-starttls \
-v \
-f \
+cc +bc \
-auth-login \
-user \
-pass 'SuPerSEKret' \
-t "" \
-sub "Test From the Commandline" \
-M "Hello World!"

Note that those backslashes are not necessary. You can put all of that on one long line if you want.

Also note that the "-v" tells mailsend to be verbose. Once you know it's working, change "-v" to "-q" to make it "quiet" instead.

OK, so now it's working, but it's kind of a pain. First you should move "mailsend" somewhere in your $PATH.

Next, I made a TextExpander shortcut, which will prompt me to fill in the To, Subject, and Message fields whenever I type "/cls" ("command line send"). Here's the snippet:

mailsend -smtp \
-port 587 \
-starttls \
-v \
-f \
+cc +bc \
-auth-login \
-user \
-pass 'SuPerSEKret' \
-t "%fill:to%" \
-sub "%fill:subject%" \
-M "%fill:message%"

As you can see at the top of this article, you don't get a whole lot of space to type your message, but this is mostly for short messages and isn't a replacement for a real mail client.

Update: it is possible that someone who ran 'ps' at just the right time could see your password using the above method. This would only occur if someone else was logged into your Mac when the command ran (such as via ssh). You can avoid this by setting a variable:



in your shell configuration file (i.e. .profile, .bashrc, .zshrc, etc). In addition to minimizing a potential security issue, it will also be easier to deal with password changes. The trade-off is that your password is now saved in a plain-text file on your computer. Make sure that your shell configuration file only allows you to read it, i.e.

chmod 600 ~/.bashrc