scripting mail receipt and saving attachment

I'm working on a script which is supposed to check an email account and automatically download .doc attachments, but I'm having some problems.

I'm using Mutt to check the email account (IMAP), and I can manually get the attachment and saving it, but I cannot for the life of me work out how to script this, as it involves selecting individual attachments from a list and specifying where to save them?

Any pointers on where to look, or alternatives to Mutt which make this easier?

The work flow is:

Check for new messages.
For each new message - parse the Subject field
make directory under /tmp/mail/ with subject as name (Subject is autogenerated and length controlled)
save attached .doc file in newly created directory
Mark all messages read.
Email reply with synopsis.

The emailing of the synopsis can be done by a script which handles the saved files, I'm adding it only in the interest of completeness.

Thanks for any help you can give me.

Stefan

You shouldn't be trying to automate an interactive program like MUTT. You should be checking the contents of your ~/.maildir yourself, or wherever MUTT keeps its mail. Being descended from a local-mail program, MUTT ought to be keeping its emails in their ordinary plain-text format.

I'm a bit rusty with ~/.maildir things, but:

# Create new timestamp file
touch ~/.lastmail2

if [ -f  ~/.lastmail" ]
then
        # Find everything newer than the last timestamp file
        find ~/.maildir -type f -newer ~/.lastmail > /tmp/$$
else
        # Find all mail files
        find ~/.maildir -type f > /tmp/$$
fi

# Replace the old timestamp file with the new
mv ~/.lastmail2 ~/.lastmail

# Read the list of files line-by-line.
while read FILE
do
        echo "Processing mail file $FILE"
done < /tmp/$$

rm /tmp/$$

"Subject:" ought to be a line by itself. I'm less sure how to extract attachments, it probably involves base64 or uuencode. If you mailed a small harmless attachment to yourself, and attached the resulting file found in your .maildir, I could probably work something out.

Thanks for the reply.

I thought that Mutt probably was not the right tool to do this with.

The problem with looking at the mailspool directly is that it is an IMAP folder on a remote mailserver. Would it be better to use something like fetchmail to get the mail and process it locally?

I'm pretty happy with grepping for the subject line, but like you, the attachment is what I'm not sure of.

Thanks for the code above anyway, but I was thinking of doing it a little differently.

I would parse the contents of the mailspool and get the info I needed, and then copy the contents of the mailspool to an archive folder and tar+gzip it.

Either way would work I guess.

I almost suggested fetchmail then (incorrectly) reasoned that MUTT must be storing the mail locally. :slight_smile: If it's not, then fetchmail's what I'd do.

I ended up using fetchmail, procmail and munpack, and get exactly what I wanted.

Thanks for your replies.