Mac Hacking: Sending Email From the Command Line

header-cover

In moving an automated process from a linux box over to a mac, I found myself missing cron’s built-in ability to send an email as I switched over to OSX’s launchd. The obvious answer, sending an email from within my bash script, send me looking for an easy way to send an email from the command line, without needing to install sendmail or postfix. Ideally I’d use Gmail as an SMTP service, but not a hard requirement.

A number of articles cited msmtp as a good solution, and this article provided a great overview of how to install it. Furthermore, it supports Gmail and can use the OSX keyring for account info, which are nice. However, in the 5 years since it was written, enough things had changed that I couldn’t follow it directly. Specifically:

  1. I ended up using Macports, since I was missing pkg-config (and probably other stuff) that would have let me build msmtp from source.

  2. Thawte made some cert changes in 2010, and the described config doesn’t work as written.

So here was my process:

  1. Installed msmtp via Macports, including the openssl variant (‘port install msmtp +openssl‘).

  2. Installed the curl-ca-bundle from Macports to replace Thawte.

  3. Created an msmtprc file as described in the earlier instructions, and store at /opt/local/msmtprc. The only change is the location of the cert file, which should now be:

    tls_trust_file /opt/local/share/curl/curl-ca-bundle.crt

  4. Manually added the account to the OSX keyring and added a .mailrc as described in the earlier instructions.

Works great!