Wednesday, February 6, 2013

Configuring emacs to send iCloud mail on Mac OS X

Pic from ajc1 on Flikr
It's handy to be able to send emails from emacs, and this guide will show how to set up SMTP via an iCloud email account.

Step 1. Install gnutls

iCloud requires you to send emails over secure channel, and emacs supports sending email with starttls or gnutls. gnutls is available through brew

To install it is easy:

brew install gnutls

Wait a few minutes while your Mac gets hot downloading and compiling!

Step 2. Create an authinfo file

emacs can look in a file ~/.authinfo to find your login credentials, so create that file and fill in the blanks.

touch ~/.authinfo
chmod 600 ~/.authinfo

The contents of the file should read:

machine smtp.mail.me.com port 587 login YOURNAME@icloud.com password YOURPASSWORD
Step 3. Configure emacs

Add the following to your .emacs file:


(setq
 send-mail-function 'smtpmail-send-it
 message-send-mail-function 'smtpmail-send-it
 user-mail-address "YOURNAME@icloud.com"
 user-full-name "YOUR FULLNAME"
 smtpmail-starttls-credentials '(("smtp.mail.me.com" 587 nil nil))
 smtpmail-auth-credentials  (expand-file-name "~/.authinfo")
 smtpmail-default-smtp-server "smtp.mail.me.com"
 smtpmail-smtp-server "smtp.mail.me.com"
 smtpmail-smtp-service 587
 smtpmail-debug-info t
 starttls-extra-arguments nil
 starttls-gnutls-program (executable-find "gnutls-cli")
 smtpmail-warn-about-unknown-extensions t
 starttls-use-gnutls t)

Note that your gnutls program may be in a different spot. Find it with:

mdfind -name gnutls-cli 
Step 4. Testing

To compose an email C-x m

Enter an email and hit C-c c to send it.

If it works, great! If not switch to the *Messages* buffer for hints on what may have gone wrong.

Step 5. Sending emails from elisp code



(message-mail recipient subject)
(message-send-and-exit)))))