noob: Trying perl, and decoding MIME attachments..stuck in my code..can someone take a look?

C

cayenne

Hello all,

I'm trying to learn Perl, and do something fairly difficult at the
same time for work. I'm trying to use the MIME tools modules...and
the examples there on the docs are kinda sparse...and I'm confused how
to decode things to a directory on my harddrive.

At this point, just to learn how it works...I'm sending the perl
script a simple email with text message...and one .jpg image that is
encoded in base64. I'm trying to write these out to a spot on my
harddrive. I'm posting just the part where it is looping through the
mime message, and trying to split off the plain text part (which does
work), but, blows up on messages with a base64 attachment.

Code snippet:

foreach $part ($entity->parts_DFS) {

$head = MIME::Head->new;
$head = $part->head;
$rec_filename = $head->recommended_filename;
$encode_type = $head->get('Content-Transfer-Encoding',0);
$encode_type =~ tr/A-Z/a-z/;
$encode_type =~ s/\s+$//;


my $mimetype = $part->mime_type();

if ($mimetype eq 'text/plain'){

$msg_body = $part->as_string;
$filename = $subject;
open (outfile,">$dir_path/$dir_name/$filename");
print outfile "$msg_body\n";
close (outfile);

} else {

$msg_body = new MIME::Decoder 'base64' or die 'unsupported';
$filename = $rec_filename;
open (outfile,">$dir_path/$dir_name/$filename");
$msg_body->decode ($part,outfile);
close (outfile);

}

I'm using :

use MIME::parser;
use MIME::Head;
use MIME::Body;
use MIME::Decoder;

And the dir_path, dir_name are all being set correctly earlier in the
code..but, I'm having difficulty in decoding the attachment to the
disk...

Can someone give me an example or describe what I'm doing wrong
here...? I think it is the output part writing out the base64
part...but, I'm not sure what is wrong here...all the examples I've
seen are going to STDOUT rather than to a file on the harddrive...

Oh, one last thing probably important I'm doing this all in 'core'
which I am guessing means just all in memory rather that a temp spot
on the disk:

$parser->output_to_core(1);

Thanks in advance for any advice, pointers, links...

chilecayenne
 
B

Brian McCauley

I'm trying to learn Perl,

You should always declare all variables as lexically scoped in the
smallest applicable lexical scope unless you have a positive is a
reason to do otherwise. BTW: this is not perculliar to Perl, it
applies in all programming languges - allowing that a languge not
having lexical variables is a positive reason :).

For Perl this means that most of the time the declaration should be
combined with the first assignment. BTW: this to is not perculliar to
Perl, it also applies in other programming languges where assignment
and declaration can be combined.

By following this convention you will be able to get maximum beniefit
out of putting "use strict" at the top of all your scripts.

Try to get into this habit now, do not wait for your failure to do so
to cause you the unecessary distress of wasting your own time and that
of other people. The longer you leave it the harder you will find it
to adjust. Worse still, if you leave it too long you may never adjust
and may mutate into a bitter and twisted troll.

The "use warnings" pragma can also be a lot of help when programming in Perl.

Have you seen the posting guidelines that are posted here frequently?
and do something fairly difficult at the same time for work.
I'm trying to use the MIME tools modules...and
the examples there on the docs are kinda sparse...and I'm confused how
to decode things to a directory on my harddrive.

At this point, just to learn how it works...I'm sending the perl
script a simple email with text message...and one .jpg image that is
encoded in base64. I'm trying to write these out to a spot on my
harddrive.

Is it failing? If so what error code is it returning when it fails?

Have you seen the posting guidelines that are posted here frequently?
I'm posting just the part

You should always try to reduce your problem to a minimal but complete
script.

Have you seen the posting guidelines that are posted here frequently?

where it is looping through the
mime message, and trying to split off the plain text part (which does
work), but, blows up on messages with a base64 attachment.

I'm sure it doesn't blow up. It probably fails in some way.

Have you seen the posting guidelines that are posted here frequently?
Code snippet:

[ snip ]

I won't do the job of a machine. Please post code that's already
strict/warnings clean if you want sentient entities to take the time
to look at it.
And the dir_path, dir_name are all being set correctly earlier in the
code..but, I'm having difficulty in decoding the attachment to the
disk...

Can you describe the form that difficulty takes?

Have you seen the posting guidelines that are posted here frequently?
Can someone give me an example or describe what I'm doing wrong
here...?

Have you seen the posting guidelines that are posted here frequently?
Thanks in advance for any advice, pointers, links...

Have you seen the posting guidelines that are posted here frequently?

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
C

cayenne

You might want to check your newsreader...you seem to have a bug that
causes massive repetition in your posts..."Have you seen the posting
guidelines that are posted here frequently?"

Yes, I read them...and not sure what I did that upset you so much. I
also read in them that not following them super strictly is not a
reason to berrate someone over it..
:)

That being said, thanks for the reply. I'm trying to write something
while reading and learning at the same time...time constraints on me
don't allow for slow learned study on this one...will go back to that
after I get this running. Thanks for the use strict suggestion...I'll
go look up what that means and does..


The part that it dies on is in the else part of the statement:

} else {

$msg_body = new MIME::Decoder 'base64' or die 'unsupported';
$filename = $rec_filename;
open (outfile,">$dir_path/$dir_name/$filename");
$msg_body->decode ($part,outfile);
close (outfile);

}

The error message I get is:

(Command died with status 21: "/home/testmail/scripts/mail_mime.pl".
Command output: Can't use an undefined value as a symbol reference at
/usr/lib/perl5/site_perl/5.8.0/IO/Wrap.pm line 35, <STDIN> line 1958.
)

I'm not sure what is not declared here...?? This is just a snippet of
a down and dirty script I'm putting together from reading of examples
on the web and the books I have.

I'm basically interested if someone can enlighten me on the MIME::
Decode module how to write out a file you decode?

Thanks for the help...
Help and constructive criticism appreciated...but, please don't
berrate. I'm just under the gun, and trying to learn and get something
working at the same time...and its not like I've not been doing
research...I'm looking all over and have not seem much out there to
work from...

TIA,

CC

Brian McCauley said:
I'm trying to learn Perl,

You should always declare all variables as lexically scoped in the
smallest applicable lexical scope unless you have a positive is a
reason to do otherwise. BTW: this is not perculliar to Perl, it
applies in all programming languges - allowing that a languge not
having lexical variables is a positive reason :).

For Perl this means that most of the time the declaration should be
combined with the first assignment. BTW: this to is not perculliar to
Perl, it also applies in other programming languges where assignment
and declaration can be combined.

By following this convention you will be able to get maximum beniefit
out of putting "use strict" at the top of all your scripts.

Try to get into this habit now, do not wait for your failure to do so
to cause you the unecessary distress of wasting your own time and that
of other people. The longer you leave it the harder you will find it
to adjust. Worse still, if you leave it too long you may never adjust
and may mutate into a bitter and twisted troll.

The "use warnings" pragma can also be a lot of help when programming in Perl.

Have you seen the posting guidelines that are posted here frequently?
and do something fairly difficult at the same time for work.
I'm trying to use the MIME tools modules...and
the examples there on the docs are kinda sparse...and I'm confused how
to decode things to a directory on my harddrive.

At this point, just to learn how it works...I'm sending the perl
script a simple email with text message...and one .jpg image that is
encoded in base64. I'm trying to write these out to a spot on my
harddrive.

Is it failing? If so what error code is it returning when it fails?

Have you seen the posting guidelines that are posted here frequently?
I'm posting just the part

You should always try to reduce your problem to a minimal but complete
script.

Have you seen the posting guidelines that are posted here frequently?

where it is looping through the
mime message, and trying to split off the plain text part (which does
work), but, blows up on messages with a base64 attachment.

I'm sure it doesn't blow up. It probably fails in some way.

Have you seen the posting guidelines that are posted here frequently?
Code snippet:

[ snip ]

I won't do the job of a machine. Please post code that's already
strict/warnings clean if you want sentient entities to take the time
to look at it.
And the dir_path, dir_name are all being set correctly earlier in the
code..but, I'm having difficulty in decoding the attachment to the
disk...

Can you describe the form that difficulty takes?

Have you seen the posting guidelines that are posted here frequently?
Can someone give me an example or describe what I'm doing wrong
here...?

Have you seen the posting guidelines that are posted here frequently?
Thanks in advance for any advice, pointers, links...

Have you seen the posting guidelines that are posted here frequently?
 
B

Brian McCauley

(e-mail address removed) (cayenne) vomits TOFU in my face:
You might want to check your newsreader...you seem to have a bug that
causes massive repetition in your posts..."Have you seen the posting
guidelines that are posted here frequently?"

Yes, I read them...and not sure what I did that upset you so much. I
also read in them that not following them super strictly is not a
reason to berrate someone over it..

But you evidently missed the bit about how rude top-posting is
considered. Or are you intending to be perceived as rude?
That being said, thanks for the reply. I'm trying to write something
while reading and learning at the same time...time constraints on me
don't allow for slow learned study on this one...

There are certain bits of "learned study" where the payoff is so great
and so soon that they are actually worth taking the time out even when
you are short of time. An analogy I like is that what you are saying
is "I'm a novice climber - I don't have time to figure out these ropes
I need to get the the top as quickly as possible". If you are
exeptionally lucky you'll get there without a serious slip. But only
if you are exeptionally lucky. Even a single fall that could have
been avoided by the ropes will often cost you an order of magnitude
more time than getting a basic idea of using the ropes would have
cost.
The part that it dies on is in the else part of the statement:

} else {

$msg_body = new MIME::Decoder 'base64' or die 'unsupported';
$filename = $rec_filename;
open (outfile,">$dir_path/$dir_name/$filename");
$msg_body->decode ($part,outfile);
close (outfile);

}

The error message I get is:

(Command died with status 21: "/home/testmail/scripts/mail_mime.pl".
Command output: Can't use an undefined value as a symbol reference at
/usr/lib/perl5/site_perl/5.8.0/IO/Wrap.pm line 35, <STDIN> line 1958.
)

I'm not sure what is not declared here...??

What makes you think there's a declaration problem?
I'm basically interested if someone can enlighten me on the MIME::
Decode module how to write out a file you decode?

At a guess (having never used MIME::Decoder) I'd say you do it as per
the documentation.

But I expect that's not your problem. I'll guess[1] that your problem is
how to pass an IO as handle as argument to a subroutine (which, of
course, as nothing to do with MIME::Decoder). BTW this is FAQ: "How
can I pass/return a {Function, FileHandle, Array, Hash, Method,
Regex}?"

$msg_body->decode($part,\*outfile);

[1] I have to guess - you've not given my a minimal but complete
script with which to check.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top