How to configure Emacs smtp for using a secure server (gmail)
This is a question originally posed on https://answers.launchpad.net/vm/+question/108267 on 2010-04-26.
It asks how one should configure Emacs mail sending packages on MS Windows for use with secure SMTP server such as gmail, which require TLS and STARTTLS. The original question is copied below.
I installed Cygwins gnutls.
-
In .emacs wrote:
(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 'starttls) (setq starttls-use-gnutls t) (setq smtpmail-debug-info t) (setq smtpmail-debug-verb t) (require 'smtpmail)
I created "~/.authinfo" file with gmail credentials.
I removed starttls.elc, so the compiled version won't be loaded by emacs.
-
I replaced
(signal-process (process-id process) 'SIGALRM)
to(call-process "g:\\www\\cygwin\\bin\\kill.exe" nil nil nil "-ALRM" (format "%d" (process-id process)))
I used the description from: http://obfuscatedcode.wordpress.com/2007/04/26/configuring-emacs-for-gmails-smtp/
- When I try to send the mail, it says in minibuffer: "Sending failed SMTP error"
In the debug buffer I get an error:
'Process SMTP exited abnormally with code 53'
that doesn't tell me anything useful.
The following is advice that led to the question of the moment. The advice was unearthed by Uday Reddy and can be found at http://article.gmane.org/gmane.emacs.windows/3250. It is dated 8/7/2006.
The problem boils down to the fact that the command (signal-process (process-id process) 'SIGALRM) does not work with Win32 Emacs, even with Cygwin also installed. But one can mimic that with: (call-process "c:\\cygwin\\bin\\kill.exe" nil nil nil "-ALRM" (format "%d" (process-id process)))
According to the documention of gnutsl-cli (e.g. http://www.gnu.org/software/gnutls/manual/html_node/gnutls_002dcli-Invocation.html#gnutls_002dcli-Invocation):
starttls option (-s)
This is the "connect, establish a plain session and start tls." option. The TLS session will be initiated when EOF or a SIGALRM is received.
Bug #7789, discussed at newsgroup gnu.emacs.bug, reports this signaling deficiency in MS Windows (aka Woe32).
I also followed the advice to make this change (in function "starttls-negotiate-gnutls" in file "starttls.el") but I was still unsuccessful in sending a test email via the server smtp.gmail.com:587.
I determined that a fix is to change the line in function "smtpmail-via-smtp" in file "smtpmail.el" that reads:
(setq process (smtpmail-open-stream process-buffer host port))
to
(let ((coding-system-for-read 'raw-text-unix))
(setq process (smtpmail-open-stream process-buffer host port)))
This insures that no coding conversion takes place when the server response is inserted into "process-buffer". In particular, it ensures that the CRLF character pair at the end of the 220 greeting from the server does not get altered.
With this one additional change I was successful in sending a test email via smtp.gmail.com:587.
My Emacs version information is "GNU Emacs 23.3.1 (i386-mingw-nt5.1.2600) of 2011-03-10 on 3249CTO".
I later found that my fix is covered by the discussion at http://comments.gmane.org/gmane.emacs.devel/140976. The general title of that discussion is "Changing the default for `send-mail-function'" (it began on 6/26/11). The changes discussed there--that make the fix--made it into Emacs 24, but are not present in Emacs 23.3 or 23.4.
It looks like Emacs 24 (currently on pre-release) has made several improvements and things are a lot simpler. First of all, the smtpmail
library can now do plain SSL. There is no need to STARTTLS. And, gmail SMTP supports SSL on port 465.
So, here are the settings required:
(setq smtpmail-stream-type 'ssl)
(setq smtpmail-smtp-server "smtp.gmail.com")
(setq smtpmail-smtp-service 465)
The authentication credentials (login and password) should go into a file ~/.authinfo or ~/.authinfo.gpg. (They cannot be put in Emacs variables any more.) There, you need to include a line of the form
machine smtp.gmail.com login ..... password ..... port 465
That is all there is to it.