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:
- Create an the file
.authinfoin your home directory. - Insert the following:
machine smtp.gmail.com login [your name]@gmail.com password [your actual password]
- 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.
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.
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.
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.
Did that happen to you on a mac? It has only happened to me on a mac.
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….
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.
May also be a good idea to reference a source distribution for starttls: http://josefsson.org/emacs-smtp-starttls.html
I would like to see a continuation of the topic
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.
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.
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
@Kjetil,
Did you try installing gnutls instead of starttls? Another commenter suggested trying that.
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.
@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?
Hey… sending mail works fine now, but what about reading mail with rmail, emacs and gmail, can’t figure this out.
Any suggestions??
Chris
@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
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.
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
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.
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)
Thanks for this! Worked first try, no sweat.
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…
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
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.
For me it wasn’t working til I did what Daniel suggested (setq starttls-use-gnutls nil) then everything worked fine.
Thanks!
It works out of the Box!
–
Regards,
Paul
Thank you,
I had been trying for a couple of hours and found this.
Works perfectly
great post, can you add a step ’0′ to add stattls.
—-
NOTE: You will need starttls, which is used to set up the SSL encrypted connection to the GMail server. This is contained in the “gnutls-bin” package on debian and ubuntu. If you don’t have this installed, you’ll get a (rather generic) “smtpmail-send-it: Sending failed; SMTP protocol error” error message.
@chavey:
I’m not sure what you mean — the first step I talk about is installing starttls. I also say that gnutls is sufficient. What exactly do I need to add?
For reference on Fedora 11, I had to install the `gnutls-utils` package to obtain the gnutls-cli program that the starttls package wants to use in emacs.
For Mac OS X, As mentioned in the Bill Clement’s blog, you can install the starttls package from Fink !!! It worked after that.
Actually, this was all done on OS X, I just don’t mention it in the post. (Although I think I was, and still am, using macports instead of Fink.) It should work with any starttls from a package manager. But of course I haven’t tested them all.
Great job.It works 4 ubuntu karmic
Thank you for your useful information.
Works great for me! I used Fink to install starttls.
It works perfectly on my Ubuntu Karmic! Thanks for the tip!
Pingback: Sending Email in Emacs | Josh Braun's Blog
This is helpful, thanks. I am not successful installing starttsl. The gnutsl option is working for me.
One thing to add to the discussion is this: Many repositories connect at port 25 instead of 587. So if the above doesn’t work for you, change all 587s to “25.”
thanks
Nice post. In my case, I had to tweek something here:
- Install gnutls-bin (debian)
- Adjust gmail application-specific password
Remember to check *messages* buffer after sending.
Now it fine.
{}’s
Pingback: How to configure Emacs smtp for using a secure server (gmail) | appsgoogleplus.com
Pingback: How to configure Emacs smtp for using a secure server (gmail) | PHP Developer Resource
Hi there, just became alert to your blog via Google, and found
that it is truly informative. I am gonna be careful for brussels.
I’ll appreciate if you continue this in future. Numerous other folks shall be benefited out of your writing.
Cheers!