Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^5: MIME::Lite and Multipart

by jakobi (Pilgrim)
on Sep 29, 2009 at 19:38 UTC ( [id://798183]=note: print w/replies, xml ) Need Help??


in reply to Re^4: MIME::Lite and Multipart
in thread MIME::Lite and Multipart

using use strict and then skipping it for a small code scrap is A TRAP. And this one got both of us.

Re^3 actually _is_ the answer you were looking for. Let's rephrase it a bit, and I'll use your original scrap for speed's sake:

diff -u
--- x 2009-09-29 21:00:01.392812366 +0200 +++ perl.scrap.mime-lite-mail-sending 2009-09-29 17:56:51.900791614 + +0200 @@ -1,23 +1,27 @@ +use MIME::Lite; +$html="<b>testhtml</b>"; +$text="testtext"; my $message = MIME::Lite->build ( From =>'someemail', - To =>'anotheremail', # a present from peter to postfix :) + To =>'jakobi', Subject=>'somesubject', Type =>'multipart/alternative' ); # this is the if(1) note if ('i have a attachment') { $message->replace(Type => 'multipart/mixed'); $message->attach( Type=> 'AUTO', # this is your major issue: **Path being undefined** - Path=> $upload_file, + Path=> '/etc/hosts', Filename=>$basename, Disposition => 'attachment' ); } # this might be a prob or might be no prob; # but usually it's text/plain in emails - $message->attach(Type=> 'TEXT', + $message->attach(Type=> 'text/plain', # TEXT Data=> $text, ); $message->attach(Type=> 'text/html', Data=> $html ); $message->send('smtp','000.000.000.000', Debug=>1,Timeout=>60,Port=>26);

Now why did you run in trouble with your scrap from Re^4: To me it looks like ::Lite HATES to be invoked without a Type. Resulting in an excess dummy mime part with undef'd data (this is a new, different bug apart from our initial issue), which isn't silently suppressed by send (finally triggering the same abort as before). Which IMHO should be considered a bug in Mime::Lite.

Note the use of Data::Dumper for debugging (or use perl -d and suitable breakpoints, but I'm too lazy for that)

--- y.orig 2009-09-29 21:11:50.544939894 +0200 +++ y 2009-09-29 21:31:44.072812281 +0200 @@ -4,27 +4,49 @@ # --------- use strict; use warnings; +use vars; use diagnostics; +use Data::Dumper; use MIME::Lite; use Email::Valid; # If we have a valid email address. - my $to_email = 'jakobi'; - my $email_valid = Email::Valid->new(mxcheck => 1,tldcheck + => 1); - if ($email_valid->address($to_email)) { + my $to_email = 'jakobi@anuurn.compact'; + #my $email_valid = Email::Valid->new(mxcheck => 1,tldcheck + => 1); + #if ($email_valid->address($to_email)) { + if (1){ + +# data dumper always shows an extra undef entry ??? +# bless( { +# 'SubAttrs' => {}, +# 'Parts' => [], +# 'FH' => undef, +# 'Attrs' => { +# 'content-disposition' => 'inline', +# 'content-type' => 'text/plain', +# 'content-length' => undef, +# 'content-transfer-encoding' => 'binary' +# }, +# 'Path' => undef, +# 'Data' => undef, +# 'Header' => [] +# }, 'MIME::Lite' ), +# and undef's seem to kill the send later on. +# to avoid this, **SET THE TYPE in ->build**! + + # Everything looks good, make up our message my $message = MIME::Lite->build ( From => $to_email, To => $to_email, Subject => 'This is the subject', - #Type => 'multipart/alternative' If I default to th +is then it never gets updated with a attachment. + Type => 'multipart/alternative' # If I default to t +his then it never gets updated with a attachment. ); # Did we get a attachment? - my $attachment = '/tmp/38293132401.pdf'; + my $attachment = '/etc/hosts'; + if (-e $attachment) { #$message->replace(Type => ''); # Tried this - #$message->replace(Type => 'multipart/mixed'); # Tr +ied this - $message->add(Type => 'multipart/mixed'); + $message->replace(Type => 'multipart/mixed'); # Tri +ed this + #$message->add(Type => 'multipart/mixed'); $message->attach( Type => 'AUTO', Path => $attachment, @@ -32,16 +54,22 @@ Disposition => 'attachment' ); } else { - $message->add(Type => 'multipart/alternative'); + # $message->add(Type => 'multipart/alternative'); } + my $text_email = 'yadda yadda yadda'; my $html_email = '<em>yadda</em> <strong>yadda</strong> <u>ya +dda</u>'; # Do up the message - $message->attach(Type => 'TEXT', + $message->attach(Type => 'text/plain', # TEXT, but seems i +nnocent and translates to text/plain Data => $text_email ); $message->attach(Type => 'text/html', Data => $html_email ); - $message->send('smtp','0.0.0.0',Debug=>1,Timeout=>60,Port=>26 +); - +warn "attach $attachment\n"; +warn "attach $attachment exists\n" if -e $attachment; +print Dumper($message); + my $rc=$message->send('smtp','0.0.0.0',Debug=>1,Timeout=>60,P +ort=>26); + # not reached, as we die due to lack of eval{} around send an +yway!? + die "send returned not true" if not $rc; +}

Replies are listed 'Best First'.
Re^6: MIME::Lite and Multipart
by Analog (Novice) on Sep 29, 2009 at 21:15 UTC
    Thanks Jakobi, I understand the issues with the first bit of code I posted, I didn't realize the importance of making sure it was actual code that could be copied, pasted and run.

      If you intend "actual" (in "I didn't realize the importance of making sure it was actual code that could be copied, pasted and run.") to mean your entire problem script then you err. What the Monks need to see is a minimal, compilable script that exhibits the same problem as your larger script.

      Don't forget to play a bit with your 2nd scrap and my diff against it. Have a look at the output of Data::Dumper, esp. the parts of the data structure containing the mime sub-parts and the effects on those when you move the "active" ...add(Type=>...) statement around.

      That way you see the sudden appearance of the excess mime part with it's undef'd Data and the subsequent abort of send(). Dumper is a worthwile addition to your bag-of-tricks.

      cu
      Peter

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://798183]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-19 08:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found