Skip to Content

Use mailsend to send email from the Terminal

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/Terminal.app. If you're not comfortable with Terminal, this probably isn't for you.

Download the source:
curl -O http://www.openssl.org/source/openssl-1.0.0.tar.gz

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 http://www.muquit.com/muquit/software/mailsend/mailsend1.15b5.tar.gz

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 (othername@example.com) from your email account (example@gmail.com), using your Gmail password (SuPerSEKret):

./mailsend -smtp smtp.gmail.com \
-port 587 \
-starttls \
-v \
-f example@gmail.com \
+cc +bc \
-auth-login \
-user example@gmail.com \
-pass 'SuPerSEKret' \
-t "othername@example.com" \
-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 smtp.gmail.com \
-port 587 \
-starttls \
-v \
-f example@gmail.com \
+cc +bc \
-auth-login \
-user example@gmail.com \
-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:

SMTP_USER_PASS=SuPerSEKret

export SMTP_USER_PASS

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



Categories

UNIX / BSD

Update: After this article was written I learned of mstmp which I highly recommend instead of mailsend. There are times that I want my...
 

Add a Comment

*0 / 3000 Character Maximum

23 Comments

Filter by:
Swiney

@TJ When you write up msmtp, the user doesn't need to compile it. You should mention this link: http://rudix.org/packages-mn.html#msmtp well... TUAW should really write up rudix in general if it hasn't done so before. Fink and macports are good when people want to install lots of packages with multiple dependencies, but rudix offers up one-off packages of *nix software as .pkg files.

May 04 2010 at 9:01 AM Report abuse rate up rate down Reply
Jason

Yes, yes, I know. Beauty is only skin-deep; I'm so vain; blah blah blah.

But this whole process is the exact definition of (1) ugly and (2) engineer-driven design rather than user-driven design.
This is not easy, and it probably shouldn't be, but there's always a gateway for a user to become a command line wizard.
(And also, the best engineers I know tend to be the worst users I have to work with. Alas…)

True of the command line in general, I'm sure, and I'm totally a devout bash scripter, but seriously. Yuck.

May 03 2010 at 9:08 PM Report abuse rate up rate down Reply
Karl

Screw that.

Check out Pony (ruby gem). By the time you you read all the above, I could've sent 10 emails.

http://github.com/adamwiggins/pony

May 03 2010 at 6:01 PM Report abuse rate up rate down Reply
Drew Crawford

I grew really tired of messing with SMTP settings and trying to configure things around SPAM filters.

In response to this mess, I wrote a little open-source script that sends push notifications to my iPod Touch/iPhone/iPad. You can grab it at http://github.com/drewcrawford/Phone-Pipe.

It's way easier than wrestling with compiling OpenSSL.

May 03 2010 at 5:45 PM Report abuse rate up rate down Reply
2 replies to Drew Crawford's comment
Jason

Your life (well, your iPhoneOS-based device) begs for PROWL. :)
You'll thank me later, I hope.

May 03 2010 at 9:04 PM Report abuse rate up rate down Reply
Drew Crawford

Prowl's command line support is really shoddy, especially cross-platform.

May 03 2010 at 9:06 PM Report abuse rate up rate down Reply
Edward Patel

I have been using msmtp successfully http://msmtp.sourceforge.net/

May 03 2010 at 5:35 PM Report abuse rate up rate down Reply
TJ Luoma


Yes, sendmail would work if you wanted to get into that.

Personally I consider this a lot more straightforward than mucking around with postfix/sendmail.

May 03 2010 at 3:35 PM Report abuse rate up rate down Reply
Tom

By the way, following these instructions will make your gmail password visible in your command line history and to anyone who can see your processes using `ps -e`.

May 03 2010 at 2:23 PM Report abuse rate up rate down Reply
1 reply to Tom's comment
TJ Luoma


That's true, but:

1) I'm going to guess 95%+ of the Mac users out there are not running with more than one person logged in.

2) I'm also guessing that anyone who wants to do this already knows enough about the commandline to understand that.

May 03 2010 at 3:31 PM Report abuse rate up rate down Reply
daenney

Why are we telling people to download, compile and install OpenSSL when it is already available on their Macs? I get that you might want a newer version but certainly for less experienced users this might cause unexplainable conflicts later on.

switch:~ daenney$ which openssl
/usr/bin/openssl

switch:~ daenney$ openssl version
OpenSSL 0.9.8l 5 Nov 2009

Also, for configuration and compilation to succeed people need to install the Developer Tools, might want to mention that.

May 03 2010 at 2:15 PM Report abuse rate up rate down Reply
3 replies to daenney's comment
colouroflight

Whoa, a post about real computing from TUAW instead of the same old iPad rubbish. Sweet.

May 03 2010 at 1:26 PM Report abuse rate up rate down Reply
Michael Baierl

sendmail works perfectly fine after setting postfix up to use a relayhost...

May 03 2010 at 11:50 AM Report abuse rate up rate down Reply
Buy an ad here

Hot Apps on TUAW

Tweets

© 2012 AOL Inc. All Rights Reserved.