in reply to MIME::Lite and Multipart

Your code obviously fails due to a lack of use MIME::Lite, some quotes, and assignments to the variables $text, $html, $upload_file, and optionally also $basename.

Add those and the mails created do look pretty normal to me in mutt.

Btw 1: don't forget to test-run the truncated example before posting - just a quick change before posting is a nice way to add errors!

Btw 2: if your if('...attachment...') is intentional, add a comment. Otherwise an if(1) is more readable.

Replies are listed 'Best First'.
Re^2: MIME::Lite and Multipart
by Analog (Novice) on Sep 29, 2009 at 17:35 UTC
    Sorry, I was just trying to make it readable. I've found the problem is simply I can't set the "Type=>xxx" after the object is created, whenever I try I simply get "no data in this part" and if I set it and try to update it after using "add" or "replace", it doesn't change. I have things working but I have to create the MIME::Lite object each time for each type I want to send (instead of being able to build it as I go).

      Hmm. I kind of ignored the 'no data', as I HAD PROPER data/mails and no errors as soon as I had your scrap working with the bits you omitted.

      below is postfix' impression of 2 runs of your code

      Sep 29 17:39:15 anuurn postfix/smtpd[18818]: connect from localhost[12 +7.0.0.1] Sep 29 17:39:15 anuurn postfix/smtpd[18818]: 5B1D9404F6: client=localh +ost[127.0.0.1] Sep 29 17:39:15 anuurn postfix/smtpd[18818]: lost connection after DAT +A (506 bytes) from localhost[127.0.0.1] Sep 29 17:39:15 anuurn postfix/smtpd[18818]: disconnect from localhost +[127.0.0.1] Sep 29 17:39:15 anuurn postfix/cleanup[18821]: 5B1D9404F6: message-id= +<20090929153915.5B1D9404F6@anuurn.compact> Sep 29 17:39:53 anuurn postfix/smtpd[18818]: connect from localhost[12 +7.0.0.1] Sep 29 17:39:53 anuurn postfix/smtpd[18818]: A5E45404F6: client=localh +ost[127.0.0.1] Sep 29 17:39:53 anuurn postfix/smtpd[18818]: lost connection after DAT +A (506 bytes) from localhost[127.0.0.1] Sep 29 17:39:53 anuurn postfix/smtpd[18818]: disconnect from localhost +[127.0.0.1] Sep 29 17:39:53 anuurn postfix/cleanup[18821]: A5E45404F6: message-id= +<20090929153953.A5E45404F6@anuurn.compact>

      update META: keeping this one in <pre> intentionally; or is the line-broken version really preferrable even for non-code illustration not intended for use/reuse?? -> there's a nice profile setting to not wrap code lines on viewing. Less pain for me. (And you, if you've similar profile settings).

      Actually this doesn't happen, if you comment out the attachment block: it sends a proper email.

      With the attachement code, you see some debug output and the 'no data' at the very point my postfix reports loss of connection. What happens is that ::Lite happily emits a mail header, blank line and possibly the text and html variable content as mime parts (you did set them, right?). Which are the first 500 or so bytes of the partial message.

      However, ::Lite doesn't check in advance that it can create a proper email and it is NOW ::Lite aborts IF THE ATTACHEMENT FILE doesn't exist(?) or is undef(your scrap!). Which is what postfix is unhappy about and thus postfix rejects the already received partial mail as well.

      So with the variables being undef, I of course did never see a mail nor the exact problem scenario you have. With the missing bits added all I see is proper mails and no problems at all.

      Summary: use strict; use warnings; use diagnostics; would have pinpointed the trouble for both of us: the undefined (or not found?) $upload_file among others, which lead to your 'no data' observation. Also testing the rc of the send operation would have made the issue more obvious.

      needless to say, I also did the mistake to test the code w/o the use strict; ...

        I actually do use all those things, Its just all part of a larger mod_perl script and it isn't as easy to make workable snippet out of.

        But if that would do it, here it is... I have the same exact problem when I run the below, whenever I try to set the type as I go, it fails with that "no data in this part". If I set the type to "multipart/alternative" when I create "my $message...." it works (but I don't get the attachment if there is one because it doesn't get switched to multipart/mixed)

        #!/usr/bin/perl -w ###################################################################### # Libraries # --------- use strict; use warnings; use diagnostics; use MIME::Lite; use Email::Valid; # If we have a valid email address. my $to_email = 'youremail@somedomain.com'; my $email_valid = Email::Valid->new(mxcheck => 1,tldcheck + => 1); if ($email_valid->address($to_email)) { # 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 thi +s then it never gets updated with a attachment. ); # Did we get a attachment? my $attachment = '/tmp/38293132401.pdf'; if (-e $attachment) { #$message->replace(Type => ''); # Tried this #$message->replace(Type => 'multipart/mixed'); # Tri +ed this $message->add(Type => 'multipart/mixed'); $message->attach( Type => 'AUTO', Path => $attachment, Filename => '38293132401.pdf', Disposition => 'attachment' ); } else { $message->add(Type => 'multipart/alternative'); } my $text_email = 'yadda yadda yadda'; my $html_email = '<em>yadda</em> <strong>yadda</strong> <u>yad +da</u>'; # Do up the message $message->attach(Type => 'TEXT', Data => $text_email ); $message->attach(Type => 'text/html', Data => $html_email ); $message->send('smtp','###.###.###.###',Debug=>1,Timeout=>60,P +ort=>26); }