This sentence is false

functional programming, software, and emacs.

Configuring Emacs for Gmail’s SMTP over SSL

When I finally convinced emacs to send mail with Gmail it was only after I had visited 6 or 7 websites on the topic, and inferred the solution. I’m posting here in hopes that at least one resource has all the information in one place. The task is to get Emacs to send mail using your Gmail account, over SSL.

Install starttls (gnutls)

According to Fink, starttls is a “simple wrapper program for STARTTLS protocol”, which is what implements SMTP-over-SSL. This step was the neglected one of all the blog entries I read on this topic. Even the emacs error messages given when attempting to send email without starttls available don’t indicate that it’s necessary or even missing. I had to poke around smtpmail.el to find out that such a program even existed.

Update 16 Jan 2008: As mentioned in the comments, installing gnutls also works. Update 09 Dec 2008: You may also need to customise the variable starttls-use-gnutls (loaded by the starttls package); for example:

(setq starttls-use-gnutls t)

Configure emacs

I eventually settled on the following settings for my .emacs:

(setq send-mail-function 'smtpmail-send-it
      message-send-mail-function 'smtpmail-send-it
      smtpmail-starttls-credentials
      '(("smtp.gmail.com" 587 nil nil))
      smtpmail-auth-credentials
      (expand-file-name "~/.authinfo")
      smtpmail-default-smtp-server "smtp.gmail.com"
      smtpmail-smtp-server "smtp.gmail.com"
      smtpmail-smtp-service 587
      smtpmail-debug-info t)
(require 'smtpmail)

The smtpmail package is included in emacs 22. I’m not sure if it is included with any earlier version.

The file .authinfo is in netrc format. It’s for storing your authentication information. Here are some quick-start directions:

  1. Create an the file .authinfo in your home directory.
  2. Insert the following:
    machine smtp.gmail.com login [your name]@gmail.com password [your actual password]
    
  3. Set its permissions so only your user can read and write it. I did this with:
    chmod go-rwx .authinfo
    chmod u+rw .authinfo
    

Now, you should be able to run emacs and test it out by composing an email with C-x m, and sending with C-c C-c. Please feel free to post a comment if you have problems. And be sure to set the variable smtpmail-debug-verb to t while you’re debugging: it makes the *Messages* buffer include much more useful information.

Update 20 Jul 2007: Removed newlines in suggested .authinfo contents; I think it doesn’t parse correctly, at least under certain conditions.

Update 13 Dec 2008: Reworked the configuration so it always uses .authinfo — apparently Google doesn’t like it any other way. This came about because I tried using the non-.authinfo config on my machine, and Gmail kept rejecting mail to send. As soon as I started using .authinfo, it worked.

26 April 2007 Posted by dbueno | emacs, howto | , | 30 Comments