Skip to content

Instantly share code, notes, and snippets.

@dubcl
Last active March 2, 2023 21:03
Show Gist options
  • Save dubcl/e8d3c285cb0c984a251c02af920bffe4 to your computer and use it in GitHub Desktop.
Save dubcl/e8d3c285cb0c984a251c02af920bffe4 to your computer and use it in GitHub Desktop.
mutt + gmail + oauth2

you need a working gpg key, this is to encrypt the gmail.token file

set you gpg key id in the line 47

ENCRYPTION_PIPE = ['gpg', '--encrypt', '--recipient', 'YOUKEYHERE']

Note: Example how to get you key

mkdir ~/.mutt
cd .mutt
curl https://gitlab.com/muttmua/mutt/-/raw/master/contrib/mutt_oauth2.py -o mutt_oauth2.py
chmod +x mutt_oauth2.py

edit mutt_oauth2.py and add the client_id and client_secret (line 61, 62) data from thunderbird app

        'client_id': '406964657835-aq8lmia8j95dhl1a2bvharmfk3t1hgqj.apps.googleusercontent.com',
        'client_secret': 'kSmqreRr0qwBWJgbf5Y-PjSU',

save the file

execute the follow

./mutt_oauth2.py gmail.token --authorize

and complete:

endpoint: google
oauth flow: localhostauthcode
email: gmailuser@gmail.com

that return an URL, copypaste the URL in a browser and click on "Allow", you need an already logged gmail user

the command will close automatically and write the file "gmail.token" in the folder

to test the token use:

./mutt_oauth2.py oauth2_token_file --test --verbose

you can see something like this:

Access Token: blablabla
IMAP authentication succeeded
POP authentication succeeded
SMTP authentication succeeded

the mutt config must be like this:

set realname = "YOU FULL NAME"
set from = "gmailuser@gmail.com"
set use_from = yes
set envelope_from = yes

set smtp_url = "smtps://gmailuser@gmail.com@smtp.gmail.com:465/"
set imap_user = "gmailuser@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/mutt_oauth2.py /path/to/.mutt/gmail.token"
set smtp_authenticators = ${imap_authenticators}
set smtp_oauth_refresh_command = ${imap_oauth_refresh_command}

run mutt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment