msmtp, a free tool to send email from Terminal
I fully expected that my article on mailsend would lead to several "Why don't you use use XYZ instead?" replies, and it did. Suggestions included Ruby, Python, iPhone push notifications, and configuring postfix/sendmail.
But one suggestion was to use msmtp, and that turned out to be the golden nugget.
What makes msmtp so great, especially compared to mailsend, is that it completely eliminates the need to store your Gmail password in a plain text file on your computer. msmtp uses the Mac OS X Keychain instead. The other big advantage is that mailsend required several command line arguments every time, which makes it very likely that someone (i.e. "me") is going to screw it up. Once msmtp is configured, I can use the venerable "/usr/bin/Mail" to send email from the command line, and /usr/bin/Mail is nearly idiot proof. (Note I said "nearly" -- this is not a challenge!)
In short: msmtp was what I was looking for when I found mailsend.
Although configuring msmtp took about 30 minutes, it was well worth it, and now that you have these handy instructions, it should take you even less time than it took me. (You can also get mstmp from Rudix or MacPorts but I still like building my own whenever possible. You might not share my neuroses, however.)
Read on for a complete walk-through.
1) Download and unarchive the source from http://sourceforge.net/projects/msmtp/ ("msmtp-1.4.20.tar.bz2" is the current version as of this writing). In order to compile the app from source, you will need to have the Mac OS X Developer Tools (Xcode) installed.
2) Run "./configure" being sure to use the "--with-ssl=openssl" and "--with-macosx-keyring" options. I actually chose to install it to my ~/Dropbox/ folder so all of my computers would have access to it and use the same configuration file, so I also added "--prefix=$HOME/Dropbox/" which made my "configure" command look like this:
./configure --prefix=$HOME/Dropbox/ --with-ssl=openssl --with-macosx-keyring
Note: if you install to Dropbox, binaries are placed in ~/Dropbox/bin/ which you will need to add to your $PATH. Dropbox does not save file permissions, so I also have my shell configuration file (.zshrc) run "chmod 755 ~/Dropbox/bin/*" every time a new shell is opened. If you do not want to use Dropbox for this, I recommend using this instead:
./configure --prefix=/usr/local/ --with-ssl=openssl --with-macosx-keyring
3) Once that has finished, type "make install". It should complete without error.
4) You need to have security certificates installed locally. I was not sure if these were already installed somewhere and wanted to make sure I had current information, so I went to https://www.thawte.com/roots/index.html and downloaded the latest version (you have to fill out a simple web form). I saved the file "thawte-roots.zip" to ~/Dropbox/ and then double-clicked to open/unarchive it.
5) The ~/Dropbox/etc/msmtprc (or /usr/local/etc/msmtprc) file is a system-wide configuration file for using mstmp. This is where the benefits over mailsend start to become apparent, as you can define several different accounts if you wish. For our purposes I am going to assume that you want to use a Gmail account to send. Replace "example@gmail.com" with your Gmail address wherever shown. Here is my file:
# Begin msmtprc
# Set default values for all following accounts.
defaults
tls on
logfile ~/.msmtp.log
# A first gmail address
account example@gmail.com
host smtp.gmail.com
port 587
protocol smtp
auth on
from example@gmail.com
user example@gmail.com
tls on
tls_starttls on
# this next line is crucial: you have to point to the correct security certificate for GMail.
# If this doesn't work, check the mstmp documentation
# at http://msmtp.sourceforge.net/documentation.html for help
#
# This next line should all be on one long line:
tls_trust_file ~/Dropbox/Thawte Roots/Thawte SSLWeb Server Roots/thawte Premium Server CA/Thawte Premium Server CA.pem
# Set a default account
# You need to set a default account for Mail
account default : example@gmail.com
# end msmtprc
6) Create a ~/.mailrc file including the full path to msmtp on your system (again, this is for Mail). This is mine:
set sendmail="/Users/yourusername/Dropbox/bin/msmtp"
Be sure to change "yourusername" to whatever your login name is on your Mac.
7) Launch "/Applications/Utilities/Keychain Access.app/" and create a new password item (File » New Password Item) that looks like this.
Note that the "Account Name" in Keychain must match the "account" listed in msmtprc exactly. Use your full Gmail address for both and you ought to be safe.

8) Test from the commandline:
echo "Hello world" | Mail -s "msmtp test at `date`" example@gmail.com
(If you have a 2nd email account, use that instead of emailing it to your own Gmail account.)
The first time you run it, you will get a Keychain authorization request for msmtp. Be sure to select "Always Allow":

Voilà! After a few fairly simple configuration steps, you can now use "Mail" or invoke msmtp directly (see "msmtp --help" for details). Your password is secure, and you can use different accounts if you wish.
Share
Categories
I fully expected that my article on mailsend would lead to several "Why don't you use use XYZ instead?" replies, and it did. Suggestions...
Add a Comment
Is there a similar way to use the OSX keyring with fetchmail (or any mail retrieving service) so that I can use mutt/pine without clear text PWs?
June 08 2010 at 6:05 PM Report abuse Permalink rate up rate down Reply(EDIT - URl messed up)
You can replace parts 1 - 3 (software installation) with mxcl's homebrew (imho far superior to macports, but choose your tool)
Install Homebrew (http://mxcl.github.com/homebrew/):
curl http://gist.github.com/raw/323731/25f99360c7de3f72027d8fd07cb369b1c8756ea6/install_homebrew.rb -o /tmp/install_homebrew.rb
ruby /tmp/install_homebrew.rb
Install msmtp:
brew install msmtp
You can replace parts 1 - 3 (software installation) with mxcl's homebrew (imho far superior to macports, but choose your tool)
Install Homebrew (http://mxcl.github.com/homebrew/):
curl http://gist.github.com/raw/323731/HEAD/install_homebrew.rb -o /tmp/install_homebrew.rb
ruby /tmp/install_homebrew.rb
Install msmtp:
brew install msmtp
Hm. I'm getting errors 71, 1, and 2 when installing msmtp - and I have no idea what to do...
May 04 2010 at 1:45 PM Report abuse Permalink rate up rate down ReplyYes, I suggest Homebrew. MacPorts leaves way too much extra stuff lying around -- binaries and libraries already installed on your machine. Homebrew installation is simple:
brew install msmtp
As a bit of an addendum, I just pushed the latest version of msmtp (the one referenced in this post) to homebrew, it should be available in a few hours.
Nothing in this post is impossible however with the version currently accessed via homebrew install.
Compiling code yourself? Psh.
That's way too open. I don't trust anything unless it comes from the App Store, which is why I replaced my 8-core Mac Pro with an iPad.
Or you could just use mail.
May 04 2010 at 9:57 AM Report abuse Permalink rate up rate down ReplyHAHAHAHAHAHAHA!!!!!! I was waiting for someone to say it!
May 04 2010 at 8:46 PM Report abuse Permalink rate up rate down Reply
Gee, why didn't I think of that?
Oh right, I did, and it didn't work.
Or you could use homebrew
May 04 2010 at 9:46 AM Report abuse Permalink rate up rate down ReplyFYI -- If you do not like compiling code etc, juts use Macports.
msmtp is one of the available ports.
Deals of the Day
more deals- $15 Apple iTunes Gift Card for $8 for new Saveology customers
- Philips Fidelio Docking Speaker Station for iPhone / iPod for $38 + $6 s&h
- Retro 80's Case for iPhone for $11 + $2 s&h
- HHI 360 Dual-View Stand Case for new iPad w/ $2 credit for $12 + $3 s&h
- HHI ReElegant Smart Cover Companion Case for new iPad from $5 + $3 s&h
- Used Apple iPad 64GB WiFi + 3G for $240 + free shipping
15 Comments