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 "firstname.lastname@example.org" with your Gmail address wherever shown. Here is my file:
# Begin msmtprc
# Set default values for all following accounts.
# A first gmail address
# 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 : email@example.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:
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`" firstname.lastname@example.org
(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.
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...
Subscribe to Newsletter
Software Updatesmore updates
- Daily App: MyScript Calculator solves your hand-written math equations
- Findery app lets you discover the world around you using annotated notes and maps
- The Learnist app brings its crowd-sourced collection of information to your iPhone
- My cat Cinnamon reviews Friskies Cat Fishing 2
- Photo Grid Collage Maker is capable and free
- iExit gets new features and is now free