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 port 587 login password YOURPASSWORD
Step 3. Configure emacs

Add the following to your .emacs file:

 send-mail-function 'smtpmail-send-it
 message-send-mail-function 'smtpmail-send-it
 user-mail-address ""
 user-full-name "YOUR FULLNAME"
 smtpmail-starttls-credentials '(("" 587 nil nil))
 smtpmail-auth-credentials  (expand-file-name "~/.authinfo")
 smtpmail-default-smtp-server ""
 smtpmail-smtp-server ""
 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)


lawlist said...

Thank you very much -- worked perfectly, with my only chosen modification being a macports installation of gnutls (instead of homebrew). I had previously done some Googling and been intimidated by problems reported by various users in configuration and usage. Your post made the process seem less daunting, and in fact it was a piece of cake thanks to the easy to follow instructions. Greatly appreciated !!!!

lawlist said...

(setq user-full-name "[Firstname Lastname]")

I have my machine named HOME, so I chose to add my name to the Emacs e-mail configuration instead of modifying my machine name.

lawlist said...

It appears that the official documentation on-line at relating to signatures may need to be updated. The latest developmental build of Emacs (i.e., post 24.3, pre 24.4) is using the variables "message-signature" and "message-signature-file", instead of the prefix "mail". The default is to include a signature from the .signature file in the home directory. To turn that off, the setting would be:

message-signature nil

To change the default location for the .signature file in the home directory, I used the following:

message-signature-file "~/path/to/file"

FYI: The settings in this blog also work for other smtp servers -- e.g., liquidweb in my case.

Justin said...

Hi lawlist. Thank you for your feedback and adding additional information.

The Geeks said...

hi...Im student from Informatics engineering nice article,
thanks for sharing :)

The Geeks said...

hi...Im student from Informatics engineering nice article,
thanks for sharing :)

Unknown said...

This works without any trouble. Great, thank you!
However, when I try to do the same thing with Wanderlust (multiple account setup), i just cannot get it to work the same way and the starttls communication fails.

Has anybody achieved a solution using Wanderlust such that one can switch between diffrent smtp-servers?

Anonymous said...


This appeared to work first time but subsequently it failed with a message 'wrong type argument: stringp nil'

Any suggestions greatly appreciated.

regards and thanks for posting