How to set up Mutt with Gmail using OAuth2 authentication

Updated

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.

  1. As the root user, install the mutt, gpg, pinentry, and python38 packages:

    ~]# dnf install mutt gpg pinentry python38
    
  2. Generate a new GPG2 key:

    ]$ gpg --gen-key
    
  3. Download the mutt_oauth2.py script:

    ]$ wget https://gitlab.com/muttmua/mutt/-/raw/master/contrib/mutt_oauth2.py
    
  4. 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.

  5. Edit the mutt_oauth2.py file as follows:

    • set YOUR_GPG_IDENTITY (your email address) in the ENCRYPTION_PIPE line
    • set your client_id and client_secret identifiers
        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': '',
            },
    
  6. Export the GPG_TTY environment variable by adding the following lines to the .bashrc file:

    GPG_TTY=$(tty)
    export GPG_TTY
    
  7. Obtain and print a valid OAuth2 access token.

    1. Run the mutt_oauth2.py script and provide a path to a (not-yet-created) token file.

      ~]$  mutt_oauth2.py <TOKEN_FILE> --verbose --authorize
      
    2. Follow the questions and directions the script will prompt you with.

      • google
      • authcode
      • copy link into browser and allow access to your account
      • obtain the code and paste it back into script
  8. Test the token:

    ~]$ mutt_oauth2.py <TOKEN_FILE> --verbose --test
    ~]$ mutt_oauth2.py <TOKEN_FILE> --verbose --debug --test
    
  9. Create the ~/.mutt/muttrc file 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}
    
  10. Run the mutt command:

    ~]$ mutt
    

Mutt is now connected to your mail account.

Additional resources

Article Type