Go Back   DaemonForums > Miscellaneous > Guides

Guides All Guides and HOWTO's.

Thread Tools Display Modes
  #1   (View Single Post)  
Old 24th January 2011
J65nko J65nko is offline
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 3,116
Thanked 182 Times in 149 Posts
Default Using fetchmail as IMAP client to download mail from a Gmail folder(label)

Gmail allows you to tag your mail with a label, which roughly corresponds to an IMAP folder.

In the following example, used under CentOS (yes, I know it is not BSD), I wanted te retrieve mail from my BUP folder from my Gmail account.
I use this folder/label to hold a BackUP from configuration files, like Xorg.conf and scripts. Very handy if you install OpenBSD binary snapshots on a regular basis.

Fetchmail can be used from the command line, but using a configuration file is much more comfortable.
The default name of the configuration file is '.fetchmailrc'

poll imap.gmail.com protocol IMAP 
   user "xxxxxxx@gmail.com" is adriaan here
   password 'mysecretpassword'
   folder 'BUP'
   fetchlimit 1
An explanation :
  • connect to 'imap.gmail.com' using the IMAP protocol,
  • login as 'xxxxxxx@gmail.com', who happens to be 'adriaan' on this machine.
  • The password for 'xxxxxxx@gmail.com' is 'mysecretpassword'
  • Only bother with the mails tagged with the 'BUP' label.
  • Fetch only one message
  • Don't delete the message from Gmail after downloading, but keep or leave it on the Gmail server.
  • Use SSL to connect to the imap.gmail.com server

For initial testing I use 'fetchlimit 1', which only will fetch a single mail.
You also can use 'fetchall'

After creating the .fetchmailrc file in the home directory, we adjust the permissions:
$ chmod g=,o= .fetchmailrc

$ ls -l .fetchmailrc
-rw------- 1 adriaan adriaan 149 Jan 24 04:50 .fetchmailrc
Remember we have a password in there!

Running fetchmail with the '-v' option to see any possible problems:

$ fetchmail -v

fetchmail: 6.3.6 querying imap.gmail.com (protocol IMAP) at Mon 24 Jan 2011 04:53:13 AM CET: poll started
Trying to connect to
fetchmail: Issuer Organization: Google Inc
fetchmail: Issuer CommonName: Google Internet Authority
fetchmail: Server CommonName: imap.gmail.com
fetchmail: imap.gmail.com key fingerprint: F4:3C:F0:30:6A:03:D4:5B:C8:5B:6E:11:ED:19:1D:A6
fetchmail: IMAP< * OK Gimap ready for requests from l51if3839987wed.40
fetchmail: IMAP> A0001 CAPABILITY
fetchmail: IMAP< A0001 OK Thats all she wrote! l51if3839987wed.40
fetchmail: IMAP> A0002 LOGIN "xxxxxx@gmail.com" *
fetchmail: IMAP< A0002 OK xxxxxx@gmail.com authenticated (Success)
fetchmail: IMAP> A0003 SELECT "BUP"
fetchmail: IMAP< * FLAGS (\Answered \Flagged \Draft \Deleted \Seen)
fetchmail: IMAP< * OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen \*)]
fetchmail: IMAP< * OK [UIDVALIDITY 595246558]
fetchmail: IMAP< * 262 EXISTS
fetchmail: IMAP< * 0 RECENT
fetchmail: IMAP< * OK [UIDNEXT 281]
fetchmail: IMAP< A0003 OK [READ-WRITE] BUP selected. (Success)
fetchmail: IMAP< * SEARCH 21 22 23 24 25 26 27 28 29 30 31 32 34
35 37 38 40 42 43 44 49 57 92 93 95 96 111 119 120 124 125 126 127
128 129 130 131 132 133 134 135 136 147 149 150 151 152 154 155 170
172 173 223 243 244 246 257
fetchmail: IMAP< A0004 OK SEARCH completed (Success)

262 messages (205 seen) for xxxxxx@gmail.com at imap.gmail.com (folder BUP).
skipping message xxxxxx@gmail.com@gmail-imap.l.google.com:1 not flushed
skipping message xxxxxx@gmail.com@gmail-imap.l.google.com:2 not flushed
skipping message xxxxxx@gmail.com@gmail-imap.l.google.com:3 not flushed
skipping message xxxxxx@gmail.com@gmail-imap.l.google.com:17 not flushed
skipping message xxxxxx@gmail.com@gmail-imap.l.google.com:18 not flushed
skipping message xxxxxx@gmail.com@gmail-imap.l.google.com:19 not flushed
skipping message xxxxxx@gmail.com@gmail-imap.l.google.com:20 not flushed

fetchmail: IMAP> A0005 FETCH 21:120 RFC822.SIZE
fetchmail: IMAP< * 21 FETCH (RFC822.SIZE 1321)
fetchmail: IMAP< * 22 FETCH (RFC822.SIZE 2427)
fetchmail: IMAP< * 23 FETCH (RFC822.SIZE 1892)
fetchmail: IMAP< * 24 FETCH (RFC822.SIZE 1571)
fetchmail: IMAP< * 25 FETCH (RFC822.SIZE 2119)


fetchmail: IMAP< * 118 FETCH (RFC822.SIZE 27921)
fetchmail: IMAP< * 119 FETCH (RFC822.SIZE 9195)
fetchmail: IMAP< * 120 FETCH (RFC822.SIZE 9193)
fetchmail: IMAP< A0005 OK Success
fetchmail: IMAP> A0006 FETCH 21 RFC822.HEADER
fetchmail: IMAP< * 21 FETCH (RFC822.HEADER {916}
reading message xxxxxx@gmail.com@gmail-imap.l.google.com:21 of 262 (916 header octets)
After having read the email from, fetchmail will now hand it over to the local mailer for deliver to the 'adriaan' account.

Trying to connect to
fetchmail: SMTP< 220 vintrax.utp.xnet ESMTP Sendmail 8.13.8/8.13.8; Mon, 24 Jan 2011 04:53:17 +0100
fetchmail: SMTP> EHLO vintrax.utp.xnet
fetchmail: SMTP< 250-vintrax.utp.xnet Hello localhost.localdomain [], pleased to meet you
fetchmail: SMTP< 250-PIPELINING
fetchmail: SMTP< 250-8BITMIME
fetchmail: SMTP< 250-SIZE
fetchmail: SMTP< 250-DSN
fetchmail: SMTP< 250-ETRN
fetchmail: SMTP< 250-AUTH DIGEST-MD5 CRAM-MD5
fetchmail: SMTP< 250-DELIVERBY
fetchmail: SMTP< 250 HELP
fetchmail: SMTP> MAIL FROM:<root@xenophanes.utp.xnet> SIZE=1321
fetchmail: SMTP< 250 2.1.0 <root@xenophanes.utp.xnet>... Sender ok
fetchmail: SMTP> RCPT TO:<adriaan@localhost>
fetchmail: SMTP< 250 2.1.5 <adriaan@localhost>... Recipient ok
fetchmail: SMTP> DATA
fetchmail: SMTP< 354 Enter mail, end with "." on a line by itself
fetchmail: IMAP< )
fetchmail: IMAP< A0006 OK Success
fetchmail: IMAP> A0007 FETCH 21 BODY.PEEK[TEXT]
fetchmail: IMAP< * 21 FETCH (BODY[TEXT] {481}
 (481 body octets)*************************
fetchmail: IMAP< )
fetchmail: IMAP< A0007 OK Success
fetchmail: SMTP>. (EOM)
fetchmail: SMTP< 250 2.0.0 p0O3rHcM004345 Message accepted for delivery
 not flushed
fetchmail: IMAP> A0008 STORE 21 +FLAGS (\Seen)
fetchmail: IMAP< * 21 FETCH (FLAGS (\Seen))
fetchmail: IMAP< A0008 OK Success
fetchmail: fetchlimit 1 reached; 261 messages left on server gmail-imap.l.google.com account xxxxxx@gmail.com
fetchmail: IMAP> A0009 LOGOUT
fetchmail: IMAP< * BYE LOGOUT Requested
fetchmail: IMAP< A0009 OK 73 good day (Success)
fetchmail: SMTP> QUIT
fetchmail: SMTP< 221 2.0.0 vintrax.utp.xnet closing connection
fetchmail: 6.3.6 querying imap.gmail.com (protocol IMAP) at Mon 24 Jan 2011 04:53:18 AM CET: poll completed
fetchmail: normal termination, status 0
You have new mail in /var/spool/mail/adriaan.
BTW the syntax of the '.fetchmailrc' configuration file is described at the end of the rather long fetchmail man page. If you finally scrolled down to this description, you could paste it into a text file for easier reference.
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote

.fetchmailrc, fetchmail, gmail folder, gmail label, imap

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Which mail client do you use? guitarscn Off-Topic 17 11th November 2010 03:12 PM
OBSD client hangs mounting NFS; Linux client doesn't amorphousone OpenBSD General 7 26th August 2010 05:21 AM
Easily retrievable configuration file backup with Gmail J65nko Guides 5 10th February 2010 04:30 AM
fetchmail: POP3< -ERR internal server error graudeejs FreeBSD General 3 19th July 2008 02:02 PM
fetchmail error, Please help!!!! cssgalactic FreeBSD General 5 29th June 2008 09:26 PM

All times are GMT. The time now is 03:21 PM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick