DaemonForums  

Go Back   DaemonForums > Miscellaneous > Guides

Guides All Guides and HOWTO's.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 24th January 2011
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,128
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'

Code:
poll imap.gmail.com protocol IMAP 
   user "xxxxxxx@gmail.com" is adriaan here
   password 'mysecretpassword'
   folder 'BUP'
   fetchlimit 1
   keep
   ssl
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:
Code:
$ 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:

Code:
$ 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 209.85.229.109/993...connected.
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 83.163.147.98 l51if3839987wed.40
fetchmail: IMAP> A0001 CAPABILITY
fetchmail: IMAP< * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA XLIST CHILDREN XYZZY SASL-IR AUTH=XOAUTH
fetchmail: IMAP< A0001 OK Thats all she wrote! l51if3839987wed.40
fetchmail: IMAP> A0002 LOGIN "xxxxxx@gmail.com" *
fetchmail: IMAP< * CAPABILITY IMAP4rev1 UNSELECT LITERAL+ IDLE
NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE
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> A0004 SEARCH UNSEEN NOT DELETED
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
[snip]
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)

[snip]

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.

Code:
Trying to connect to 127.0.0.1/25...connected.
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 [127.0.0.1], pleased to meet you
fetchmail: SMTP< 250-ENHANCEDSTATUSCODES
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
  #2   (View Single Post)  
Old 8th November 2022
shep shep is offline
Real Name: Scott
Arp Constable
 
Join Date: May 2008
Location: Dry and Dusty
Posts: 1,503
Default

Based on the above, I was able to expand the IMAP folders to both Inbox and SentMail and put the emails into #mh format. Leaving out @J65nko's Password line results in 2 sequential prompts for email server access.

Code:
poll mail.spectrum.net proto IMAP port 993
  user "*******@spectrum.net"
  options keep fetchall
ssl
mda "/usr/local/libexec/nmh/rcvstore +Inbox"

poll mail.spectrum.net proto IMAP port 993
  user "*******@spectrum.net"
  folder 'SentMail'
  options keep fetchall
ssl
mda "/usr/local/libexec/nmh/rcvstore +SentMail"
nmh needs an .mh_profile entry in the users home directory. You can change the Path name; eg Path: Feb2022Mail:
Code:
$ cat ~/.mh_profile                                                       
MH-Profile-Version: 1.0
Path: Mail
and ~/Mail/{Inbox,SentMail} directories. you will need an .mh_sequences file

In my first run, authorization for the "SentMail" folder failed ?timedout? I'm considering splitting the Inbox and SentMail entries in to 2 separate configuration file as fetchmail will allow alternate configuration files
Code:
fetchmail -f fmail_inboxrc 
fetchmail -f fmail_sentmailrc
To my knowledge this is the only imap sync posted that outputs into #mh format.
mail/offlineimap and mail/isync provide the option to use Maildir format.

Of interest opensmtpd natively supports Maildir and has an mda configuration option.

Last edited by shep; 14th November 2022 at 01:41 AM.
Reply With Quote
Reply

Tags
.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:20 PM.


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