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 | , | 25 Comments

25 Comments »

  1. I had many similar problem trying to get e-mail working, including the TLS problem. The fact that they neglect to tell you an external program is required is annoying.

    You can always put your username and password in .authinfo, which is an extention of the .netrc format. It’s marginally more secure, since you can set more restrictive modes on .authinfo with no ill effect.

    My remaining problem is that I can’t post news over SSL. It hangs expecting a response from the server after submitting the article, even though the server is responding correctly. I’ve only found one other person who has this problem.

    Comment by Peter | 30 April 2007 | Reply

  2. Am I that other person? Because I, too, have had that problem. Last time I tried to track it down I determined it was openssl’s fault, but I don’t remember precisely why. Emacs hangs, but the article is successfully posted. Really annoying.

    I’ll have to try that again soon, to see if the problem persists.

    Re: .authinfo

    I didn’t know about that. I may try that.

    Comment by dbueno | 30 April 2007 | Reply

  3. Well, make that three people, then.

    After some experimenting, I discovered that you can C-g the sending process, kill the buffer (C-c C-k will do it and remove the draft), and then run gnus-summary-mark-article-as-replied if you were replying to an article, and that fairly effectively works around the problem. Still annoying, though.

    Comment by Peter | 30 April 2007 | Reply

  4. Did that happen to you on a mac? It has only happened to me on a mac.

    Comment by dbueno | 30 April 2007 | Reply

  5. Thanks for putting this here. I wasted a lot of time before I found your explanation and duly installed starttls. Amazing that no one else pointed that out….

    Comment by Bar | 16 May 2007 | Reply

  6. Hmm. When I post news with Thunderbird on Mac OS X 10,4, it hangs for a while and gives me an error message, but the message is successfully posted.

    Comment by Chris | 31 October 2007 | Reply

  7. May also be a good idea to reference a source distribution for starttls: http://josefsson.org/emacs-smtp-starttls.html

    Comment by Andrei | 07 December 2007 | Reply

  8. I would like to see a continuation of the topic

    Comment by Maximus | 20 December 2007 | Reply

  9. Oddly, on OSX 10.4, I had to install gnutls to get things to work. But now they do work, and it is all thanks to you.

    Comment by skillet-thief | 21 December 2007 | Reply

  10. It was greatly useful for me. I am just taking my first steps towards using Emacs as my mail software. I had setup rmail for POP3 and now this SMTP setup completes the circle.

    However, I was not able to setup the ~/.authinfo file. I have written exactly in the format that you have given here, but smtpmail was not able to read it properly it seems. The *Messages* says: buffer has smtpmail-send-it: Sending failed; SMTP protocol error.

    Comment by Venkatesh | 12 January 2008 | Reply

  11. Hello,

    I have tried for several hours but without success. — The prosess fails after I have typed in my password:

    smtpmail-send-it: Sending failed; SMTP protocol error

    Here is my *trace file:

    ———————–trace—————–
    Process SMTP deleted
    220 mx.google.com ESMTP z34sm1604008ikz.8

    EHLO kjetils-maskin.lan

    250-mx.google.com at your service, [85.165.85.228]

    250-SIZE 28311552

    250-8BITMIME

    250-STARTTLS

    250 ENHANCEDSTATUSCODES

    MAIL FROM: SIZE=210

    530 5.7.0 Must issue a STARTTLS command first z34sm1604008ikz.8

    QUIT

    221 2.0.0 mx.google.com closing connection z34sm1604008ikz.8
    —————-trace—————-

    I hace a emacs v22 running on my Mac (os 10.5).

    ————————– dot 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
    ‘((”smtp.gmail.com” 587 “kjetipet@gmail.com”))
    ; smtpmail-default-smtp-server “smtp.gmail.com”
    smtpmail-smtp-server “smtp.gmail.com”
    smtpmail-smtp-service 587
    smtpmail-debug-info t)
    (require ’smtpmail)

    ————————– dot emacs ——————

    Do you have any advice for me?

    Best Regards, Kjetil

    Comment by Kjetil | 05 March 2008 | Reply

  12. @Kjetil,

    Did you try installing gnutls instead of starttls? Another commenter suggested trying that.

    Comment by dbueno | 10 March 2008 | Reply

  13. Hello again,

    No, as you see in the pasted dot-emacs snippet following, I am loading starttls and smtpmail.

    Regards,

    ….
    (setq mail-source-primary-source
    ‘(pop :server “pop.gmail.com”
    :password “….xxxx….”))

    (load “starttls”)
    (load-library “smtpmail”)

    (setq message-send-mail-function ‘message-smtpmail-send-it)
    (add-hook ‘message-send-mail-hook ‘mail-source-touch-pop)
    ….

    This code-snippet is located before the code I previous referred to.

    Comment by Kjetil | 10 March 2008 | Reply

  14. @Kjetil,

    I didn’t have to load any special “starttls.el” in order to get this all working. Have you tried (1) without the (load “starttls”) line above and (2) after installing gnutls?

    Comment by dbueno | 10 March 2008 | Reply

  15. Hey… sending mail works fine now, but what about reading mail with rmail, emacs and gmail, can’t figure this out.
    Any suggestions??

    Chris

    Comment by chris | 08 July 2008 | Reply

  16. @Chris,

    I, too, would like a nice way to read email with Emacs. Perhaps I’ll be motivated to learn, and post in this in the near future.

    For the time being, here’s the manual section for Rmail:

    http://www.gnu.org/software/emacs/manual/html_node/emacs/Rmail.html

    Comment by dbueno | 08 July 2008 | Reply

  17. Folks having trouble getting things working with starttls may need to:

    (setq starttls-use-gnutls nil)

    I was planning on using gnutls, but couldn’t get darwinports to build it properly. This was needed to fall back to starttls.

    Comment by David | 31 October 2008 | Reply

  18. Hey, thanks! Your post saved me hours of time setting up TLS mail-sending from Emacs. (I mean, I’d already spent hours, but I was clearly going to spend hours more… and then I found this post :-) ).

    -Karl

    Comment by Karl Fogel | 09 December 2008 | Reply

  19. Hi.

    I have been trying to configure the mail facility in Gnu Emacs 22.3.1 for several hours with success:

    Operating System:
    WinXP SP3

    Message:
    250-SIZE 35651584
    250-8BITMIME
    250-STARTTLS
    250 ENHANCEDSTATUSCODES
    530 5.7.0 Must issue a STARTTLS command first. 30sm2898275wfc.55
    221 2.0.0 closing connection 30sm2898275wfc.55
    smtpmail-send-it: Sending failed; SMTP protocol error

    Lisp code within _emacs and .gnu.el:
    (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
    ‘((”smtp.gmail.com” 587 “my_email_address@gmail.com” nil))
    smtpmail-default-smtp-server “smtp.gmail.com”
    smtpmail-smtp-server “smtp.gmail.com”
    smtpmail-smtp-service 587
    smtpmail-debug-info t)
    (require ’smtpmail)

    I installed gnutls but that didn’t resolve the issue.
    Any help would be most appreciated.

    Thanks.

    Comment by Katie | 11 December 2008 | Reply

  20. Thanks for posting this! You should considering adding it to the Emacs Wiki.

    Katie, have you tried setting startttls-use-gnutls?

    (setq startttls-use-gnutls t)

    Comment by Chris | 24 December 2008 | Reply

  21. Thanks for this! Worked first try, no sweat.

    Comment by J Cooper | 23 January 2009 | Reply

  22. For all those who still get the “must issue STARTTLS command first” error: if you are using xemacs, the solution is probably described here: http://groups.google.com/group/gnu.emacs.bug/browse_thread/thread/be1ad3235b22585f?q=vm+starttls#28770327fcb6df42

    in brief, the solution is to add the following to ’site-start.el’:
    (defun with-no-warnings (&rest body)
    “Like `progn’, but prevents compiler warnings in the body.”
    ;; The implementation for the interpreter is basically trivial.
    (car (last body)))

    just took me 4 hours of googling to discover this…

    Comment by Thomas | 26 January 2009 | Reply

  23. Bah! Humbug!

    Any ideas what might be wrong? The usual “Must issue a STARTTLS command first” nonsense.

    From *Messages*:
    250-SIZE 35651584
    250-8BITMIME
    250-STARTTLS
    250-ENHANCEDSTATUSCODES
    250 PIPELINING
    530 5.7.0 Must issue a STARTTLS command first.

    .gnus:
    (setq send-mail-function ’smtpmail-send-it
    message-send-mail-function ’smtpmail-send-it
    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)
    (require ’starttls)
    (setq starttls-use-gnutls t)

    I’ve installed starttls and gnutls, and added the “with-no-warnings” to site-start.el without effect :-(

    Comment by Himself | 21 February 2009 | Reply

  24. Using emacs 22.3, I got the same error until I changed

    (setq starttls-use-gnutls t)

    to

    (setq starttls-use-gnutls nil)

    now it works.

    Comment by Daniel Moerner | 27 May 2009 | Reply

  25. For me it wasn’t working til I did what Daniel suggested (setq starttls-use-gnutls nil) then everything worked fine.

    Comment by Anonymous Coward | 05 July 2009 | Reply


Leave a comment