Thursday, January 10, 2008

Emacs Tip #9: Gnus/Gmail integration (using IMAP)

Like many folks, I use Google for more and more. When I noticed Google's announcement that Gmail supports IMAP, I knew I could read my Gmail from Emacs.

The question was, how do you set up the integration? The Emacs wiki had instructions on how to read email, which worked nicely. But I wanted to enable Gmail to gather the mail I sent from Emacs into its conversation groups (it's no Gnus threading, but it's a start). So how to enable that? At first I automatically Bcc'ed my Gmail account, but that was hacky. Then I noticed Gmail lets you use their SMTP server.

I found some documentation on how to use it, but it didn't work for me (wrong ports evidentially), and it was integrated differently than what I wanted.

So I used my favorite Emacs lisp feature, advice, and wrote the following snippet. It checks to see if the 'From:' address is a gmail address, in which case it uses Gmail's SMTP server.

(defadvice message-send-mail (around gmail-message-send-mail protect activate)
"Set up SMTP settings to use Gmail's server when mail is from a address."
(interactive "P")
(if (save-restriction
(string-match "" (message-fetch-field "from")))

(let ((message-send-mail-function 'smtpmail-send-it)
;; gmail says use port 465 or 587, but 25 works and those don't, go figure
(smtpmail-starttls-credentials '(("" 25 nil nil)))
(smtpmail-auth-credentials '(("" 25 "" nil)))
(smtpmail-default-smtp-server "")
(smtpmail-smtp-server "")
(smtpmail-smtp-service 25)
(smtpmail-local-domain ""))

Note: You can also download the snippet directly.

And, as a good netizen, I updated the wiki appropriately.


Bob Erb said...

Thanks. "set up gnus to send through diff SMTP servers" is on my todo list. Lazy web to the rescue!

Unknown said...


Unknown said...

message-send-hook??? It's not needed for this code as this code is using advice to hook into the message-send-mail function.

When using a hook to solve this problem, you have to set the variables permanently, I like the cleanliness of having them only bound inside the 'let' statement.

Alex Ott said...

useful tip for using gnus + gmail:

(setq gnus-ignored-newsgroups "")

to enable showing of [Gmail]/* groups