I need suggestions on how can I download attachments from my IMAP mails which have attachments and current date in subject line i.e. YYYYMMDD format and save the attachments to a local path.
I went through the Perl module Mail::IMAPClient and am able to connect to the IMAP mail server, but need help on other tasks. One more thing to note is that my IMAP sever requires SSL auth.
Also the attachments could be gz, tar or tar.gz files.
A simple program that does what you want is below.
The minimum version for
Email::MIMEis for whenwalk_partswas introduced.You don’t want to hardcode your password in your program, do you?
Connect using SSL. We ought to be able to be able to do this with a simple
Sslparameter to the constructor, but some vendors have chosen to break it in their packages.If you want a folder other than the inbox, change it.
Using IMAP search, we look for all messages whose subjects contain today’s date in YYYYMMDD format. The date can be anywhere in the subject, so, for example, a subject of “foo bar baz 20100316” would match today.
For each such message, write its attachments to files in the current directory. We write the outermost layer of attachments and do not dig for nested attachments. A part with a name parameter in its content type (as in
image/jpeg; name="foo.jpg") is assumed to be an attachment, and we ignore all other parts. A saved attachment’s name is the following components separated by-: today’s date, its IMAP message ID, a one-based index of its position in the message, and its name.