scripting mail receipt and saving attachment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting scripting mail receipt and saving attachment
# 1  
Old 11-03-2011
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
# 2  
Old 11-03-2011
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:

Code:
# 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.
# 3  
Old 11-03-2011
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.
# 4  
Old 11-03-2011
I almost suggested fetchmail then (incorrectly) reasoned that MUTT must be storing the mail locally. Smilie If it's not, then fetchmail's what I'd do.
# 5  
Old 11-04-2011
SOLVED

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

Thanks for your replies.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mail attachment

Hi, I want to send an attachment and content in a single mail using unix command can anyone pls help me? Thanks Naveen A (6 Replies)
Discussion started by: Pranaveen
6 Replies

2. Shell Programming and Scripting

Mail Attachment...

Hi All, I am new to unix. I have written the script to identify the delimiter count for my source file and the output I have to capture in another file and that file should sent to mail with attachment. I have tried to send the attachment with below script . But I am not able to do that. Any... (2 Replies)
Discussion started by: suresh_target
2 Replies

3. UNIX for Dummies Questions & Answers

How to send html file in a mail not as an attachment but it should display in the mail in table for

Hi The below script working when we are sending the html as attachment can u please guide how to send thesmae data in table form direct in the mail and not in mail attachment . cat Employee.sql SET VERIFY OFF SET PAGESIZE 200 SET MARKUP HTML ON SPOOL ON PREFORMAT OFF ENTMAP ON - HEAD... (0 Replies)
Discussion started by: mani_isha
0 Replies

4. Shell Programming and Scripting

Mail attachment with unix mail

Hi Could someone help me with the details on how to send an attachment through mail in unix. I have an html file in my unix machine and I want this to be send to some mail id, plese help with the steps. Regards Ajay (2 Replies)
Discussion started by: ajaykumarboyana
2 Replies

5. UNIX for Advanced & Expert Users

mailx commannd - Mail and Attachment in same mail

Hi , I am using mailx command for sending the mails. Rightnow I am sending the attachment (by using uuencode $filename) as a seperate mail.I wanna send the attachment also with the same mail. (6 Replies)
Discussion started by: sharif
6 Replies

6. Shell Programming and Scripting

mail with attachment

Hi, I am facing an issue while sending a mail from unix with attachment.the issue is i am able to send a mail with attachment but...after receiving the mail if u see that the text is continous though it was line by line in original.. UUENCODE is the cmd i am using... plz help me in this... (1 Reply)
Discussion started by: param786
1 Replies

7. UNIX for Advanced & Expert Users

mail receipt

how can I control a mail receipt sent from unix using sendmail (4 Replies)
Discussion started by: npires
4 Replies

8. UNIX for Advanced & Expert Users

Saving of UNIX based e-mail on to local disks

Dear friends, Someone please let me know how to store e-mail on local hard disk or floppy disks. I am using UNIX based PINE e-mail programme and the copy of this mail I wanted to store it in my local disks. Thanks in advance. Regards, Rajan (4 Replies)
Discussion started by: rajan9
4 Replies

9. UNIX for Dummies Questions & Answers

mail attachment

This question has been asked many time before. I did search other questions. But was not able to work out. Really sorry for repeating the same question. I have mail and mailx and sendmail installed. It Tried this uuencode FILENAME.txt FILENAME.txt | mail mymail@yahoo.com the attachment... (3 Replies)
Discussion started by: sushrut
3 Replies
Login or Register to Ask a Question