assigning directory path to downloaded email attachments

I am trying to build a script that will download emails + their
attachments (from gmail server) and place them both into their
respective folders (one folder containing one email + its attachments).

I have managed to build a script (by putting together pieces from two
separate scripts) that gets the job 90% done, my problem is that I can't
get the attachments to save in the same folders as their respective

The folder structure where the emails are saved now is perfect, it is
just a question of getting the attachments to follow its respective
email into the folder.

If someone would have any tips for me I would be extremely grateful!

Here's some sample code of the attached script:

The filename in the mime headers is just a filename - you need to join
that with a directory name same as you do when saving the email


Well you've already got

for when you wanted a file in dir with the name email.subject, it's
exactly the same this time except that instead of email.subject it's
whatever you've extracted from the message part.