PERL script to download attachments from email

Hi, I am using a perl script to connect to my IMAP server and retrieve my unread messages. I am using Net::IMAP::Simple .. when I find an unread message, I get a full copy of the message. Here is part of my script:

for $i (@mylist) {
$msg = $server->get( $i );
print "Message Number $i contains:\n";
print "$msg\n";
$ret = `rm -f /tmp/msg.txt`;
open(TMP,"> /tmp/msg.txt");
print TMP "$msg";
close TMP;
$line1 = `grep "Subject: " /tmp/msg.txt`;
chomp($line1);

Open in new window


I can see the attachment in the "$msg" .. but this is what the attachment looks like:

Content-Type: image/jpeg; name="20190421_132835.jpg"
Content-Disposition: attachment; filename="20190421_132835.jpg"
Content-Transfer-Encoding: base64
Content-ID: <16a45e7d6e23c578d131>
X-Attachment-Id: 16a45e7d6e23c578d131

Content-Type: image/jpeg; name="20190421_132835.jpg"
Content-Disposition: attachment; filename="20190421_132835.jpg"
Content-Transfer-Encoding: base64
Content-ID: <16a45e7d6e23c578d131>
X-Attachment-Id: 16a45e7d6e23c578d131

Open in new window


The data for the JPEG follows after that.

I just downloaded that script to take a look at it. It is not doing exactly what I need to do, and the script has no documentation on how to use it and run it

What I'd like to do is to modify my script so that any attachments are downloaded into a folder.

Thanks!
Jim WestAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
https://www.perlmonks.org/?node_id=1133987 provides a good overview.

The process...

1) Parse the message in to a set of mine objects.

2) Loop through all the objects.

3) Save which ever object you like to files.
Jim WestAuthor Commented:
David,  

This script works great, and does exactly what I need.  

The script creates a directory with a very complicated name,
 I don't see in the script how the directory name is generated or where the directory is created.  

If it was possible, I would like to change the directory name to something simpler, but I'm not seeing in the script,
 how or where the directory is crated in the script.

I modified the script to set the entity to my "message" file

### Parse an MIME message in a file:
my $entity = $parser->parse_open("/tmp/msg1.txt");

With that minor change, the script works perfect.  

When I walk through the script with the perl debugger, the containing directory, and the parsed files appear immediately after the following line is executed:

my $entity = $parser->parse_open("/tmp/msg.txt");

At first, I thought maybe the files were just allocating space, but they have the correct content.
I'll have to do some reading on MIME::Parser to understand what the rest of the script is doing.

I am not familiar with the use of the perl open statement below:

          open( my $OUTFILE, ">", "output.7z" );
          print $OUTFILE "$content";
          close( $OUTFILE );

That is the only line where "ouput.7z" exists, so "output.7z" is a mystery.

The script works great.  

I'd like to understand the script more, but that is not a requirement.

Thanks!
Jim West
Jim WestAuthor Commented:
Hi David,

The author of the script was attempting to store the parsed files into a zip file.  
So, it appears all the work in the script is really done in the line:

my $entity = $parser->parse_open("/tmp/msg.txt");

... And ... I can set the output directory with:

### Output each message body to the same directory:
$parser->output_dir("/mydir/user");

Much to learn.
Thanks for introducing me to MIME::Parser !

Thanks,
Jim
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
1) The script creates a directory with a very complicated name,

Likely this is MIME::Tools magic creating complex directories to avoid any directory collision during parallel runs of same script or parsing many MIME messages together in the same script.

Likely the only way to change this is to dig into MIME::Tools... which can drive you mad... I suggest you just live with what MIME::Tools gives you.

2) The author of the script was attempting to store the parsed files into a zip file

Likely best to remove the zip file logic, as this can be slow + cumbersome to debug/fix bugs.

Also, if the bulk of your message is images, they tend to only reduce in size slightly using massive CPU time.

Likely the original author was simply using zip files as a collection management mechanism.

Be much easier to debug if you use a directory + file structure.

If you must use zip, be sure to set compression level to 0 (no compression) for fastest writing of your zip files.

3) Thanks for introducing me to MIME::Parser

You're welcome!

I've been using PERL since the original perl4 version.

PERL is always my goto tool. I can use write a PERL script faster then just figuring out how to accomplish the same effect in some other language.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim WestAuthor Commented:
David's suggestion of using MIME::Parser was exactly the solution that I needed.
Thanks,
Jim West
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Images and Photos

From novice to tech pro — start learning today.