Download mail using IMAP with fetchmail

3.8k views Asked by At

I need to download all mail messages from a mail account with fetchmail.

When I try with POP3 I can download all mail correctly in this format:

[root@srv root]# ls /home/mail_import/MAIL_USER/new/
1453828024.7837_0.srv
1453828029.7843_0.srv

But pop3 protocol don't allow to choose a folder, so i need to use IMAP.

I cannot download the mails separately when using IMAP. I tried and I have a single file with all mails.

For example:

[root@srv home]# stat /home/mail_import/MAIL_USER/teste
  File: ‘/home/mail_import/MAIL_USER/teste’

[root@srv home]# head /home/mail_import/MAIL_USER/teste
From root@SRV  Tue Jan 26 18:56:31 2016
Return-path: <root@SRV  >
Envelope-to: MAIL_USER@SRV  
Delivery-date: Wed, 02 Dec 2015 15:47:00 -0500

I need to download all mails using imap in separate files like the pop3.

My .fetchmailrc is:

set bouncemail
set no spambounce
set softbounce
set properties ""
defaults:
  antispam -1
  batchlimit 100

poll DOMAIN with proto IMAP
  user 'USER' there with password 'PASS' is 'MAIL' here
  options keep fetchall ssl mda "/usr/bin/procmail -f %F -d %T";
  folder INBOX

and my .procmailrc is:

MAILDIR=/home/mail_import/MAIL_ACCOUNT
DEFAULT=$MAILDIR/INBOX
LOGFILE=/var/log/procmail
LOCKFILE=$MAILDIR/.default.lock
VERBOSE=on
:0 fhw
|formail
#
## Any other rules the user wishes to either include with INCLUDERC,
## or hardcode into this file, would go here.
## --------------------------------------------------------------------------
## If we're here, the mail didn't match any other rules, so deliver normally.
:0:
$DEFAULT
## If that fails, report an error and throw the mail away.
EXITCODE=75
:0
/dev/null

There is some correct option to download the e-mail using IMAP separately equal POP3?

1

There are 1 answers

0
tripleee On

I don't see why you are using Procmail here at all. Just run Fetchmail and let it fetch your mail. Specify a destination folder in a suitable format, and go.

Whether or not email messages are separate files is not a feature of the protocol. It is a feature of the delivery program you use; if you choose to deliver to a file (Berkeley mbox format; what you are seeing here, with a From_ line at the beginning of every message) then all messages will be delivered to a single file. If you deliver to a folder (in maildir format, for example, with the new tmp cur subdirectories) you will get the result you are asking for. Just do whatever you did to get your POP3 messages into the maildir folder MAIL_USER, only using imap instead of pop3, and you are all set.

If you specifically want to do this in Procmail, change

DEFAULT=$MAILDIR/INBOX

to

DEFAULT=$MAILDIR/

But the entirety of your .procmailrc seems pointless. Why do you pipe stuff through formail? The actions you have simply duplicate Procmail's default behavior, with a couple of bugs. I think you could simplify both your own understanding and the process by figuring out how to have Fetchmail deliver the messages straight where you want them. (Not entirely sure whether it supports maildir, though; quick googling was inconclusive. Maybe don't specify an mda at all if that's how you made this happen with POP3.)