Here I’ll try to show how to configure Gnus to connect to your gmail account so you can read and send your e-mails without having to leave your Emacs 🙂 If you have multiple IMAP accounts I also tell you how to configure Gnus for multiple IMAP account, the same thing can be done for POP accounts, though I don’t mention POP here.
All the stuff here are slightly modified configurations found at the EmacsWiki
Every time you load gnus with the commands M-x gnus or M-x gnus-other-frame it reads the file ~/.gnus, so, all configurations mentioned here should be added to this file.
So, beginning the configuration process let’s configure our Gnus so it can fetch e-mails from your Gmail account:
(setq gnus-select-method ‘(nnimap “gmail”
This is all you need to start reading your e-mails with Gnus, note that you will have to have gnutls package installed in order to use ssl.
To test this type M-x gnus, it should ask for your login, type firstname.lastname@example.org than type your password. After that you should be redirected to the *Group* buffer, type U and subscribe to your Gmail folders. There are lot’s of options, too many to be mentioned here, please read the EmacsWiki for more information.
In order to not have to be typing your account and password you can edit a file called ~/.authinfo and add something like this:
machine imap.gmail.com login email@example.com password mypassword port 993
machine smtp.gmail.com login firstname.lastname@example.org password mypassword port 587
Change myaccount and mypassword to your account and password. The smtp line will be use for sending mail.
To add another IMAP account use the gnus-secondary-select-methods variable like this:
Unfortunately there is no native method to add multiple SMTP servers on Gnus. But after taking a quick look at the wiki I found this link. I changed it a little bit, so the passwords are searched at the ~/.authinfo file instead of being in your .gnus file.
Here is the code found on my .gnus file:
;; Available SMTP accounts.
‘((ssl “email@example.com” “smtp.gmail.com” 587 “key” nil)
(ssl “firstname.lastname@example.org” “smtp.otherserver.com” 25 “key” nil)))
This lists my SMTP accounts, one line for each server, as you can see both the servers are using SSL.
;; Default smtpmail.el configurations.
(setq send-mail-function ‘smtpmail-send-it
user-full-name “Rodrigo S. Wanderley”
The code above just sets some default values. My mail signature is found on the file ~/emacs/signature, I also specify my user name user-full-name and my e-mail address user-mail-address so that Gnus can fill the From header field automatically for me.
The Debug options is also nice so that you get some feedback about what is happening while Gnus is sending the e-mail for you.
(defun set-smtp-plain (server port)
“Set related SMTP variables for supplied parameters.”
(setq smtpmail-smtp-server server
(message “Setting SMTP server to `%s:%s’.”
server port address))
(defun set-smtp-ssl (server port key cert)
“Set related SMTP and SSL variables for supplied parameters.”
(setq starttls-use-gnutls t
smtpmail-starttls-credentials (list (list server port key cert))
“Setting SMTP server to `%s:%s’. (SSL enabled.)”
server port address))
Those are two functions used to send mail, one with and one without SSL support. Note that smtpmail-auth-credentials is telling Gnus where to find the username and password.
(defun change-smtp ()
“Change the SMTP server according to the current from line.”
(loop with from = (save-restriction
for (acc-type address . auth-spec) in smtp-accounts
when (string-match address from)
((eql acc-type ‘plain)
(return (apply ‘set-smtp-plain auth-spec)))
((eql acc-type ‘ssl)
(return (apply ‘set-smtp-ssl auth-spec)))
(t (error “Unrecognized SMTP account type: `%s’.” acc-type)))
finally (error “Cannot interfere SMTP information.”))))
(add-hook ‘message-send-hook ‘change-smtp)
Finally we create a hook for when me message is going to be sent. This hook basically tries to match the From header with the e-mail address you specified at smtp-account variable and chooses the server accordingly.