Re: Ideas to obtain text file list of emails in an IMAP folder?
Ron Leach wrote:
> List, good afternoon,
>
> Have any debian-user readers ever tried to create a list of all the
> email messages stored in an IMAP folder?
>
> We run Dovecot as an IMAP server. Email messages relating to
> different projects/activities are held in different IMAP folders. I'd
> like to have a list, in a text file, of all the messages held in one
> or other IMAP folder. The best outcome would be a file listing each
> message's From, To, Date, and Subject, and any order would do. I only
> need the list of messages, not the messages themselves. The person
> doing this would be the person whose mailbox it is, and would have the
> IMAP passwords etc.
>
> [snip]
Do the same here (Dovecot + Postfix), not 100% certain if this'll match
your setup, but it should be pretty close.
ASSUMPTIONS BASED ON MY SETUP:
| mailroot: /var/mail/vhosts/domain.tld
| userdir: $mailroot/user
| |-inbox: $userdir/cur
| |-otherfolders: $userdir/directory/cur
In the inbox (or other folder), mails are referenced as long filenames
like -- 1464813605.M917021P29754.mailserver,S=49388,W=50440:2,d (not
that it matters, but it might be important to you).
And the headers are, well, standard. So, we can pull the info out
pretty easily. This is a terribad "I just wrote it right now" script,
but I think it covers what you want.
It works here ... output looks like this:
| Message: 1464237628.M592694P7866.[xxx],S=6188,W=6304:2,S
| Subject: Re: Thanks
| From: Peter
| Date: Wed, 25 May 2016 20:52:23 -0400
| To: Dan Purgert <dan@djph.net>
|
| =================
|
| [next message ...]
HTH, and happy hacking :)
< - @ - @ - CUT HERE FOR SCRIPT - @ - @ ->
#!/bin/bash
########################################################################
#
#
# script: mlhdr
# by: Dan Purgert
# copyright: 2016
# version: 0.1
# date: Fri Jun 3 11:33:44 EDT 2016
# purpose: Quick and dirty script to help fellow
# : hacker Ron Leach with getting email
# : header information.
#
# license: GPL v2 (only)
# prerequisites: User running this MUST(RFC2119) be a
# : member of sudoers (or root).
# installation: chmod +x and run via sudo/as root.
#
#
########################################################################
if [[ $EUID -ne 0 ]]; then
printf "\nSorry, this script must be run as root.\n"
exit 0
fi
if [[ $# = 0 ]] || [[ "$1" = "-h" ]] ; then
printf "
------------
mlhdr
------------
This script is intended to grab the header information from emails in
the listed directory.
Usage:
mlhdr /path/to/mail/box [/output/file]
Options:
-h Print this help and exit"
exit 0
fi
spinner() {
local pid=$1
local del=0.5
local sp='/-\|'
local n=${#sp}
printf ' ' >&2
while [ $(ps a | awk '{print $1}' | grep $pid) ] ; do
printf '\b%s' "${sp:i++%n:1}" >&2
sleep $del
done
}
if [[ "$2" = '' ]]; then
:
else
exec 1>"$2"
fi
chk=$(echo "${1:(-3)}")
if [[ "$chk" = "cur" ]]; then
dir="$1"
else
dir="$1/cur"
fi
cd "$dir"
(
for f in *; do
printf "Message: $f\n"
egrep '^From:|^To:|^Date:|^Subject:' "$f"
printf "\n=================\n\n"
done ) & spinner $!
#############################END#####################
--
Registered Linux user #585947
Github: https://github.com/dpurgert
Reply to: