17 Oct 2011

Again about emacs gnus and gmail

Sometimes ago my gnus stopped sending emails through gmail SMTP server. The message was something like "connection refused". I didn't have time to dig the problem as in the office I'm using web mail due to company proxy and quite used to use it.

But after switching completely to Emacs 24 and starting using el-get with nognus I've decided to figure out what is the problem. Quick debugging of emacs smtpmail.el and network-stream.el showed me that problem comes from gnutls-cli which stopped connecting to port 587 of smtp.gmail.com with message "Connection refused". I've also recognized that in emacs 24 developers have introduced new parameter smtpmail-stream-type.
It has the following documentation:

Connection type SMTP connections.
This may be either nil (possibly upgraded to STARTTLS if possible), or `starttls' (refuse to send if STARTTLS isn't available), or `plain' (never use STARTTLS)..

Changing value to nil, starttls didn't help. After checking usage of the variable in open-network-stream function in network-stream.el I've recognized that it could have much more values:

:type specifies the connection type, one of the following:
  nil or `network'
             -- Begin with an ordinary network connection, and if
                the parameters :success and :capability-command
                are also supplied, try to upgrade to an encrypted
                connection via STARTTLS.  Even if that
                fails (e.g. if HOST does not support TLS), retain
                an unencrypted connection.
  `plain'    -- An ordinary, unencrypted network connection.
  `starttls' -- Begin with an ordinary connection, and try
                upgrading via STARTTLS.  If that fails for any
                reason, drop the connection; in that case the
                returned object is a killed process.
  `tls'      -- A TLS connection.
  `ssl'      -- Equivalent to `tls'.
  `shell'    -- A shell connection.
Also I found that we can use port 465 instead of 587 with smtp.gmail.com. After changing my configuration in .gnus to the following I can send emails from gnus again (hooray!!!):

(setq message-send-mail-function 'smtpmail-send-it
      smtpmail-starttls-credentials '(("smtp.gmail.com" 465 nil nil))
      smtpmail-auth-credentials '(("smtp.gmail.com" 465 "name@gmail.com" nil))
      smtpmail-default-smtp-server "smtp.gmail.com"
      smtpmail-smtp-server "smtp.gmail.com"
      smtpmail-smtp-service 465
;     smtpmail-debug-verb t
;     smtpmail-debug-info t
      smtpmail-local-domain nil
      smtpmail-stream-type 'ssl)