Skip to content

Instantly share code, notes, and snippets.

@sbng
Last active April 12, 2024 06:46
Show Gist options
  • Save sbng/af4b3c65553e59f1f4ad006a035b574b to your computer and use it in GitHub Desktop.
Save sbng/af4b3c65553e59f1f4ad006a035b574b to your computer and use it in GitHub Desktop.
Using mutt with Gmail and Oauth2

mutt

mutt is a console/terminal base Email client. it is fast and flexible with many option for customization. It works well with any editor of choice. Vim, emacs and neovim are common editor that are used in mutt for composing and replying to email.

Gmail

Gmail despite a wonderful webui but lack the flexibility to manipulate email using key binding. Fortunely, Gmail does provides imap service to allow the usage of email services from third party email client. Console Email client (mutt) is an excellent alternative to read Email from console/terminal.

Using mutt to read Gmail

  1. Install mutt and downlad mutt_oauth2.py script
  2. Enable Gmail imap service with Oauth2
  3. Configure mutt

Install mutt and mutt_oauth2.py

fedora/redhat distribution

sudo dnf install mutt

debian/ubuntu distriution

sudo apt install mutt

Download Mutt Oauth2 scripts. Follow the instruction to populate the client id and client secret in the mutt_oauth2.py. Run the script with --authorize flag to obtain the refresh token.

Enable Gmail imap service with Oauth2

Login to Google cloud.

  1. Create a new project if no project exist.

  2. Select "Oauth Consent screen"

  3. Create User Type - "External"

  4. Provide detail for "App Name", "User support email", "Developer contact". The rest of the field can be left empty.

  5. Add the following three scope manaully:

    Gmail API - https://www.googleapis.com/auth/gmail.modify 
    
    Google Drive API - https://www.googleapis.com/auth/drive.appdata  
    
    People API - https://www.googleapis.com/auth/contacts.readonly 
    
  6. Enable the API access of the various scope using the following links.

    Gmail - https://console.developers.google.com/apis/api/gmail.googleapis.com/overview 
    
    Google Drive API https://console.developers.google.com/apis/api/drive.googleapis.com/overview 
    
    People API - https://console.developers.google.com/apis/api/people.googleapis.com/overview 
    
  7. Finally add a user and complete the creation of "Oauth consent screen"

  8. Select "Credentials". Create new credential for Oauth2 Client ID.

  9. Create a credntial for "Desktop app" providing the app with a new name.

  10. Note the "Client ID" and "Client Secret". This will be needed to populate the mutt_oauth2.py script under the Google registration field.

Testing of the new Oauth2 credential

Run the following command

mutt_oauth2.py gmail.tokens --verbose --authorize

Test the token to ensure the token is correct and working

mutt_oauth2.py gmail.tokens --verbose --test

Configuring muttrc

Add the following snipplet in muttrc to start using mutt with Gmail.

set realname = "<Real name>"
set from = "name <gmail id>"
set use_from = yes
set envelope_from = yes

set smtp_url = "smtps://<gmail id>@smtp.gmail.com:465/"
set imap_user = "<gmail id>"
set folder = "imaps://imap.gmail.com:993"
set spoolfile = "+INBOX"
set postponed ="+[Gmail]/Drafts"
set trash = "+[Gmail]/Trash" 
set ssl_force_tls = yes
set header_cache=~/.mutt/cache/headers
set message_cachedir=~/.mutt/cache/bodies
set certificate_file=~/.mutt/certificates
set imap_check_subscribed
set imap_keepalive = 100
unset imap_passive
set mail_check = 60
set imap_authenticators="oauthbearer:xoauth2"
set imap_oauth_refresh_command="/path/to/mutt_oauth2.py /path/of/tokens/file.token"
set smtp_authenticators=${imap_authenticators}
set smtp_oauth_refresh_command=${imap_oauth_refresh_command}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment