7

I would like to download all the e-mails in my old e-mail server. It uses POP3 and I'm interested in e-mails in "Inbox" and "Sent" folders. Once I have downloaded all the messages, I would like to make a script which lists all the e-mails between my e-mail address and one certain e-mail address in chronological fashion into single text file.

However, at first, I think I need to download all the messages to a single file with headers including dates and then create a sorting script. How to approach this?

I have used mailx in scripts for sending mails, but is it possible to use mailx to download all the mails from POP3 server into a single file?

1
  • 1
    POP3 only supports an Inbox, with no additional folders on the server. (RFC 1939 Sec 8) Sep 30, 2013 at 5:02

1 Answer 1

4

Traditional mailx does not support IMAP or POP, but the one that comes with Linux does.

For your particular problem, I recommend using fetchmail instead. You can use the --mda option to have fetchmail execute a script of your choice for each downloaded message. It can even pass the From and To addresses as parameters to your script if you use %F and %T as placeholders in the command line.

First, create a mailsorter script:

#!/bin/sh

dest_mbox="$1"
from="$2"
to="$3"

case "$from-$to" in
  [email protected])
     echo "From $from  `date`" >> "$dest_mbox"
     cat >> "$dest_mbox"
     ;;
  *)
     cat > /dev/null
     ;;
esac

Then run fetchmail -u myname popserver.example.com --mda './mailsorter /tmp/mbox %F %T'

While testing this solution, give fetchmail the --all and --keep flags to make sure that you don't delete your mail accidentally.

4
  • Thanks! Fetchmail was exactly what I was looking for. However, there is one problem- if I fetch all my e-mail over POP3, I still download only the ones I see in "Inbox" folder in web front-end. However, I would like to access e-mail in "Send" and "Deleted" folders as well. Is this possible?
    – Martin
    Dec 2, 2013 at 23:44
  • 1
    POP3 has no concept of any folder other than Inbox. Your Sent and Deleted folders probably reside locally on the client. Dec 2, 2013 at 23:55
  • 200_success, ok, I see. However, I have only used this e-mail account through web-interface. I found one way to access "Sent" and "Deleted" folders by coping e-mails from those folders to "Inbox". The other and more sane option would be probably to use IMAP if my web-mail provider supports it as IMAP should support other folders besides "Inbox"?
    – Martin
    Dec 3, 2013 at 0:02
  • IMAP does support multiple folders. Use fetchmail --folder Sent (or fetchmail INBOX.Sent with some IMAP servers' folder layout). Dec 3, 2013 at 0:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .