How to set up Mutt with Gmail using OAuth2 authentication
The Mutt email client version 2.0.7 and later includes support for OAuth2 authorization protocol. Newly, you can connect Mutt with various cloud email providers, such as Gmail, using XOAUTH2 tokens.
Follow these steps to set up Mutt with your Gmail account.
-
As the
rootuser, install themutt,gpg,pinentry, andpython38packages:~]# dnf install mutt gpg pinentry python38 -
Generate a new
GPG2key:]$ gpg --gen-key -
Download the
mutt_oauth2.pyscript:]$ wget https://gitlab.com/muttmua/mutt/-/raw/master/contrib/mutt_oauth2.py -
Obtain credentials from your account at Content from console.developers.google.com is not included.Content from console.developers.google.com is not included.https://console.developers.google.com/apis/credentials.
-
Edit the
mutt_oauth2.pyfile as follows:- set
YOUR_GPG_IDENTITY(your email address) in theENCRYPTION_PIPEline - set your
client_idandclient_secretidentifiers
ENCRYPTION_PIPE = ['gpg', '--encrypt', '--recipient', 'YOUR_GPG_IDENTITY'] DECRYPTION_PIPE = ['gpg', '--decrypt'] registrations = { 'google': { 'authorize_endpoint': 'https://accounts.google.com/o/oauth2/auth', 'devicecode_endpoint': 'https://oauth2.googleapis.com/device/code', 'token_endpoint': 'https://accounts.google.com/o/oauth2/token', 'redirect_uri': 'urn:ietf:wg:oauth:2.0:oob', 'imap_endpoint': 'imap.gmail.com', 'pop_endpoint': 'pop.gmail.com', 'smtp_endpoint': 'smtp.gmail.com', 'sasl_method': 'OAUTHBEARER', 'scope': 'https://mail.google.com/', 'client_id': '', 'client_secret': '', }, - set
-
Export the
GPG_TTYenvironment variable by adding the following lines to the.bashrcfile:GPG_TTY=$(tty) export GPG_TTY -
Obtain and print a valid OAuth2 access token.
-
Run the
mutt_oauth2.pyscript and provide a path to a (not-yet-created) token file.~]$ mutt_oauth2.py <TOKEN_FILE> --verbose --authorize -
Follow the questions and directions the script will prompt you with.
- authcode
- copy link into browser and allow access to your account
- obtain the code and paste it back into script
-
-
Test the token:
~]$ mutt_oauth2.py <TOKEN_FILE> --verbose --test ~]$ mutt_oauth2.py <TOKEN_FILE> --verbose --debug --test -
Create the
~/.mutt/muttrcfile including the following code. Replace the generic lines with your data.set realname = "test" set from = "<YOUR_EMAIL_ADDRESS>@gmail.com" set use_from = yes set envelope_from = yes set smtp_url = "smtps://<YOUR_EMAIL_ADDRESS>@gmail.com@smtp.gmail.com:465/" set imap_user = "<YOUR_NAME>@gmail.com" set folder = "imaps://imap.gmail.com:993" set spoolfile = "+INBOX" set ssl_force_tls = yes # G to get mail bind index G imap-fetch-mail set editor = "vim" set charset = "utf-8" set record = '' set imap_authenticators="oauthbearer:xoauth2" set imap_oauth_refresh_command="<PATH_TO_mutt_oauth2.py> <TOKEN_FILE>" set smtp_authenticators=${imap_authenticators} set smtp_oauth_refresh_command=${imap_oauth_refresh_command} -
Run the
muttcommand:~]$ mutt
Mutt is now connected to your mail account.
Additional resources
- Content from gitlab.com is not included.Content from gitlab.com is not included.https://gitlab.com/muttmua/mutt/-/blob/master/contrib/mutt_oauth2.py.README
- Content from www.mutt.org is not included.Content from www.mutt.org is not included.http://www.mutt.org/doc/manual/#oauth