Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > noob: Trying perl, and decoding MIME attachments..stuck in my code..can someone take a look?

Reply
Thread Tools

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

 
 
cayenne
Guest
Posts: n/a
 
      05-25-2004
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:ecoder '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:arser;
use MIME::Head;
use MIME::Body;
use MIME:ecoder;

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
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      05-25-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (cayenne) writes:

> 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\\
 
Reply With Quote
 
 
 
 
cayenne
Guest
Posts: n/a
 
      05-26-2004
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:ecoder '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 <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> (E-Mail Removed) (cayenne) writes:
>
> > 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?

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      05-26-2004
(E-Mail 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:ecoder '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:ecoder) 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:ecoder). 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\\
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Decoding MIME and UUencode. Alasdair Baxter Computer Support 4 08-14-2011 01:30 PM
When someone can't take good photos they try to take control Nomen Nescio Digital Photography 0 03-04-2009 02:40 PM
Problem with Email::MIME and Email::MIME::Attachment::Stripper ecureuil Perl Misc 0 05-28-2006 01:47 AM
decoding MIME attachments? Mad Scientist Jr ASP .Net 1 11-11-2005 09:15 PM
Decoding MIME email. Thomas Williams Python 1 08-25-2003 07:06 PM



Advertisments