DEV Community

gemmaro
gemmaro

Posted on

Easy Gmail setup with Gnus and smtpmail via OAuth2

(This is a manual paraphrase of the Japanese article. It might not be fluent English; sorry in advance.)

This article explains how to send and receive email on Emacs. Particularly, we will use Gnus for receiving, smtpmail for sending, Gmail for an email service, and OAuth2 for authentication.

Outline

Files: Emacs initialization file (e.g. ~/.emacs.d/init.el) and auth-source file (e.g. ~/.authinfo)

Steps:

  1. Emacs: Install auth-source-xoauth2-plugin.
  2. Emacs: Set Customize variables and edit authinfo.
  3. Emacs: Restart Emacs and launch Gnus. A prompt appears in the minibuffer. It also opens a confirmation page in your browser.
  4. Browser: Press Continue. It looks like an error page, but this is expected.
  5. Browser: Copy code=<CODE> query parameter from the address bar.
  6. Emacs: Paste the code in the Emacs minibuffer.

Customize variables to set. Please replace <NAME>:

(auth-source-xoauth2-plugin-mode t)
(gnus-secondary-select-methods
 '((nnimap "imap.gmail.com" (nnimap-user "<NAME>@gmail.com")
           (nnimap-server-port "imaps") (nnimap-stream tls)
           (nnir-search-engine imap) (nnimap-authenticator xoauth2))))
(message-send-mail-function 'smtpmail-send-it)
(smtpmail-smtp-server "smtp.gmail.com")
(smtpmail-smtp-service "587")
(smtpmail-stream-type 'starttls)
;; I recommend not to set browse-url-browser-function to eww,
;; and to keep smtpmail-smtp-user as default.
Enter fullscreen mode Exit fullscreen mode

Example ~/.authinfo. Please replace <NAME>:

machine imap.gmail.com login <NAME>@gmail.com port imaps auth xoauth2 auth-source-xoauth2-predefined-service google
machine smtp.gmail.com login <NAME>@gmail.com port 587 auth xoauth2 auth-source-xoauth2-predefined-service google
Enter fullscreen mode Exit fullscreen mode

That's all!

Notes

OAuth2
Gmail still support username/password authentication. So you can also configure authinfo as follows, though it is not OAuth2. Please replace <NAME> and [...].

machine smtp.gmail.com login <NAME>@gmail.com password [...] port 587
machine imap.gmail.com login <NAME>@gmail.com password [...] port 993
Enter fullscreen mode Exit fullscreen mode

auth-source file
If you set up PGP, you can also use an encrypted ~/.authinfo.gpg file. See also auth-sources Customize variable.

browse-url-browser-function
EWW is a great web browser, but here we don't use EWW because otherwise it won't automatically open a web browser (like Firefox) which supports JavaScript, so you have to copy the URL manually from the minibuffer. It can be also problematic since EWW buffer might collide with Gnus buffers.

smtpmail-smtp-user
auth-source-xoauth2-plugin package internally handles this variable, so leaving this variable unset should cause fewer problems.

How to send email?
C-x m (M-x compose-mail) to open a buffer with message major mode. Fill in the message and C-c C-c (M-x message-send-and-exit) to send the email.

References

License

Copyright (C) 2026 gemmaro

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved. This file is offered as-is,
without any warranty.

Top comments (0)