Fetchmail from Google's Gmail

Jurjen Bokma

Last modified: "2016-12-22 14:34:26 jurjen"


How to use fetchmail to retrieve mail from a Gmail account and inject it into the local mail stream, with a mere seconds lag.

Table of Contents

Configuring Fetchmail (and GMail)

Fetchmail can be used to retrieve mail via POP or IMAP, and insert it into the local MTA's incoming stream. Doing this with Google's mail is a bit harder than usual because of Google's default adherence to OAuth2.0, which was originally not intended for use with mail.

  1. I found that it worked best to have Google generate a so-called app password.


    This app password can only be generated if you leave 2-factor authentication enabled. With 2-factor authentication disabled and thus no app password, I couldn't seem to make fetchmail connect to the gmail server.

  2. You can see that 'app password' in the contents of ~/.fetchmailrc:

    poll imap.gmail.com protocol IMAP auth password
        user "apprentice@gmail.com" is apprentice here
        password 'klwpcmaqeycndist'
        ssl, sslcertck, idle

    So we can fetch mail:

    apprentice@host:~$ fetchmail -v
    fetchmail: 6.3.26 querying imap.gmail.com (protocol IMAP) at Wed 21 Dec 2016 09:00:02 AM CET: poll started
    Trying to connect to
    fetchmail: Server certificate:
    fetchmail: Issuer Organization: Google Inc
    fetchmail: Issuer CommonName: Google Internet Authority G2
    fetchmail: Subject CommonName: imap.gmail.com
    fetchmail: Subject Alternative Name: imap.gmail.com
    fetchmail: imap.gmail.com key fingerprint: 97:D0:32:78:2B:BA:AB:3A:EC:03:99:28:F2:1E:82:CC
    fetchmail: SSL/TLS: using protocol TLSv1.2, cipher ECDHE-RSA-CHACHA20-POLY1305, 256/256 secret/processed bits
    fetchmail: IMAP< * OK Gimap ready for requests from d3mb140991022edc
    fetchmail: IMAP> A0001 CAPABILITY
    fetchmail: IMAP< A0001 OK Thats all she wrote! d3mb140991022edc
    fetchmail: will idle after poll
    fetchmail: IMAP> A0002 LOGIN "apprentice@gmail.com" *
    fetchmail: IMAP< A0002 OK apprentice@gmail.com authenticated (Success)
    fetchmail: IMAP> A0003 SELECT "INBOX"
    fetchmail: IMAP< * FLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing)
    fetchmail: IMAP< * OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing \*)] Flags permitted.
    fetchmail: IMAP< * OK [UIDVALIDITY 1] UIDs valid.
    fetchmail: IMAP< * 2 EXISTS
    fetchmail: IMAP< * 0 RECENT
    fetchmail: IMAP< * OK [UIDNEXT 13] Predicted next UID.
    fetchmail: IMAP< * OK [HIGHESTMODSEQ 2624]
    fetchmail: IMAP< A0003 OK [READ-WRITE] INBOX selected. (Success)
    fetchmail: IMAP> A0004 EXPUNGE
    fetchmail: IMAP< A0004 OK Success
    fetchmail: IMAP> A0005 SEARCH UNSEEN
    fetchmail: IMAP< * SEARCH
    fetchmail: IMAP< A0005 OK SEARCH completed (Success)
    2 messages (2 seen) for apprentice@gmail.com at imap.gmail.com.
    fetchmail: IMAP> A0006 IDLE
    fetchmail: IMAP< + idling