Reading your mail inside GNU Emacs with GNus
<2026-08-29T02:58:49:00+03:00> :gnu:tech:email:emacs:
Table of Contents
Emacs is a wonderful editor which can handle many tasks related to text. It can play games, it can write you code, manage version control, help you read your system documentation, view images under a GUI, read webpages, etc. It is also known to be able to let you read Newsgroups and E-mail.
Today, I set up(again) my email inboxes in emacs. I figured it was worth documenting.
Most people nowadays use GMail, as gmail is a leading service in emails, and google accounts are easy to make and very useful, providing access to many platforms.
In Emacs, the method to read mail can be through the builtin Gnus feature, which lets you read mail like newsgroups.
For this example, we are going to be using IMAP to receive our emails, and send through SMTP. We will not be downloading a complete log of all messages(POP3 format).
1. ~/.emacs
1.1. IMAP setup
(setq gnus-secondary-select-methods
'((nnimap "gmail"
(nnimap-address "imap.gmail.com")
(nnimap-server-port "imaps")
(nnimap-stream-ssl)
(nnmail-expiry-target "nnimap+gmail:[Gmail]/Trash")
(nnmail-expiry-wait immediate)))
gnus-ignored-newsgroups "^to\\.\\|^[0-9. ]+\\( \\|$\\)\\|^[\"]\"[#'()]")
You could either set gnus-secondary-select-methods to include this, or if you only want Gnus to read your emails, do the same, except
(setq gnus-select-method
'(nnimap "gmail"
(nnimap-address "imap.gmail.com")
(nnimap-server-port "imaps")
(nnimap-stream-ssl)
(nnmail-expiry-target "nnimap+gmail:[Gmail]/Trash")
(nnmail-expiry-wait immediate))
gnus-ignored-newsgroups "...") ;; note you should use the string
;; from the last code block
In essence, the IMAP select method is supported by Gnus. Here, we're specifying GMail's specific requirements to connect:
nnimap-address- IMAP connections go to either imap.gmail.com or imap.googlemail.com, depending on what lands after the @ in your email address (e.g. user@ gmail .com)
nnimap-server-port- IMAP connects for Gmail must connect through
imaps(993). This is like https, or ftps, or other secured
connections, which is that a security layer involving SSL/TLS must
be used. Hence,
nnimap-server-port, andnnimap-stream-sslwere specified. nnmail-expiry-target- simply specifies where read mails should
go. If you would like them to not expire in the trash, and remain
in their specific directories, you can omit this. If you do want
them to expire there, include
nnmail-expiry-wait
The last set for the variable gnus-ignored-newsgroups is a failsafe to
make sure gnus doesn't ignore certain directories in your email. This
way everything is included.
1.2. SMTP setup
(setq smtpmail-smtp-server "smtp.gmail.com"
smtpmail-smtp-service 587
send-mail-function 'smtpmail-send-it)
- smtpmail-smtp-server
- sets the specific recipient server for outgoing smtp messages. Gmail receives them at smtp.gmail.com. I don't know if there is an smtp.googlemail.com
- smtpmail-smtp-service
- the specific port that gmail needs which is 587 for smtp messages.
- send-mail-function
- this line ensures that the default outgoing email message takes the protocol of smtp. If you don't set this, you will be prompted to choose from a list; Type smtp therein.
2. ~/.authinfo and passwords
Passwords need to be specified in ~/.authinfo.
If you are like me, and don't enjoy copy-pasting mindlessly, you can
view the language specification of .authinfo in the manpage for netrc.
In this scheme, we are connecting in two places: Gmail's IMAP server, and gmail's SMTP server. One server to receive messages, and the other to send. It is not one server we simply connect to.
We should write this:
machine imap.gmail.com login user@gmail.com password <app-password> port 993 machine smtp.gmail.com login user@gmail.com password <app-password> port 587
2.1. What password to use?
The password situation is a little strange.
As of the time of writing, GMail simply refuses to let third-party apps which they don't like to log into email accounts using their pure address+password.
The workaround at that point was to jump into the Google Cloud, create a faux project, and then administer a GMail API, add your address to it, then create an API key, authorise it for your app and address as if you were installing an app that did this alone, and use the API key as the password. This API key would provide authorisation through OAuth2. However, neither this works.
The current solution is little known, however Google still allows you to create 'app-passwords' which are write-only passwords you copy into an app once, and never again, and which provide complete access to your google account. This app password will prevent (1) the error from imap.gmail.com complaining this app is insecure, and (2) the error from imap.gmail.com complaining your valid api keys you created and authorised are invalid credentials. Google themselves say this is only needed for a few apps, and I guess for now GNU Emacs is one of those apps.
If you want to find out how you can create an app password for yourself, you can do so at their official guide for this.
When you get the password, write it down(without spaces) in both of the password fields in your .authinfo file. That should work.
3. Trying it out
… Et voila! It should work now.
When you have saved these files, you should do M-x load-file RET
~/.emacs RET (or just restart your emacs)
When you are ready, type M-x gnus RET
You'll see the wildebeest/bull icon. This means it's loading. When
it finishes loading, you'll see a few inboxes with unread messages in
them. You can look at inboxes with no new messages in them with L
If you would like to see all messages, say, in the inbox, then you
should type / o which will reveal messages which are read.
When you quit, type q and confirm your desire to escape Gnus (lol).
From there you should be able to remember more commands needed to operate in Gnus. The commands which work in a newsgroup will also work here in your emails, because Gnus sees the folders as newsgroups as well.