Psychodead has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks! Iīm quite desperate, as I have this nasty "no data in this part" error when sending a mail with attachment using Mime lite. It works actually, the mail is sent correctly. But of cause I donīt want our customers to see the error message. Here is my Code:
use MIME::Lite; my $msg = MIME::Lite->new( From => $p->{from}, Subject => $p->{subject}, To => $p->{to}, CC => $p->{cc}, BCC => $p->{bcc}, Type => 'multipart/mixed' ); $msg->attach( Type => 'text/plain', Data => $text_mail, Encoding => 'quoted-printable' ); $msg->attach( Type => 'application/pdf', Data => $pdf, Filename => q(buchung_).$token.q(.pdf), Disposition => 'attachment' ); $msg->send || print "you DON'T have mail!";
The variables all contain data which is sent correctly. Please make the nasty error msg dissappear ! :-) Thnx!

Replies are listed 'Best First'.
Re: Mime::lite "no data" error again
by jethro (Monsignor) on Dec 09, 2009 at 11:02 UTC

    Lets do some debugging: Throw out the first $msg->attach.... Do you see any error? Then throw out the second $msg->attach.... Do you see any error? If the error vanishes the thrown out statement is the problem

    I looked at the source of MIME::Lite and the error message gets thrown when 'Data' is empty. So check either $text_mail or $pdf whether they really contain something exactly before attach is called. Use a print statement or the perl debugger

      I just checked that. When commenting out the first attachment (where $text_mail is used), it works fine. I looked into that variable and as expected it contains my message text. Do I need quotations around the variable or something like that?

        Do you use 'use strict;'? You might have misspelled the name of the variable and strict mode will detect this.

        I don't think the text needs to be quoted, it is automatically quoted or translated to base64 by MIME::Lite.

        You could put a line with $text_mail= 'Testing'; just before the attach to check that the problem really is the contents of $text_mail

        If that works, change $text_mail to contain the first half of your message. If that works, the second half. If you find that one half doesn't work, half that again and see if you can pinpoint the problem.

Re: Mime::lite "no data" error again
by roboticus (Chancellor) on Dec 09, 2009 at 13:00 UTC
    Psychodead:

    Dubious guess: Perhaps it's whining about not having a message body? If there were a single attachment, it might be using it as the body.

    You might also check the case of your keys: The MIME::Lite doc shows "Cc" rather than "CC", so you may be missing bits of your EMail.

    ...roboticus
      Eeerm... Yeah, could be that somehow it has no content data. I made another MIME::Lite-Object, where I put my attachments. Then I attached this one to my $msg. Donīt ask me why, but it works! Looks like this:
      $part_content = MIME::Lite->new( Type => 'text/plain', Data => $text_mail, Encoding => 'quoted-printable', ); $part_content->attach( Type => 'application/pdf', Data => $pdf, Filename => q(buchung_).$token.q(.pdf), Disposition => 'attachment' ); # and then: $msg->attach($part_content); $msg->send || print "you DON'T have mail!";