Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Mail::Sendmail Multipart MIME isn't working

by cormanaz (Deacon)
on Jun 27, 2008 at 23:26 UTC ( [id://694469]=perlquestion: print w/replies, xml ) Need Help??

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

Good day bros. I am trying to get Mail::Sendmail to send a multi-part MIME message containing an HTML and a text version of the same message. I am cribbing code from from the Perl Sendmail FAQ which I have interpreted thus:
sub sendmessage { my ($html,$text,$recipient,$reportdate) = @_; my $boundary = "====" . time() . "===="; my %mail = ( from => 'foo@bar.org', to => $recipient, subject => "A message from me", 'content-type' => "multipart/alternative; boundary=\"$bou +ndary\"" ); my $plain = encode_qp $text; $html = encode_entities($html); $boundary = '--'.$boundary; $mail{body} = <<END_OF_BODY; $boundary Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable $plain $boundary Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable $html $boundary-- END_OF_BODY sendmail(%mail) || die "Error: $Mail::Sendmail::error\n"; return; }
When I run this, it sends the message fine, but when I open the message in Outlook it shows the raw HTML instead of recognizing it as HTML. Here are the relevant header fields from the mail client (with some info starred out to protect the innocent, namely me):
Return-path: <info@*****.org> Envelope-to: corman@*****.com Subject: A message from me Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="====1214608857====" To: corman@*****.com Date: Fri, 27 Jun 2008 17:20:57 -0600 From: info@*****.org
Anybody know what's going wrong here?

Steve

Replies are listed 'Best First'.
Re: Mail::Sendmail Multipart MIME isn't working
by blahblahblah (Priest) on Jun 28, 2008 at 01:13 UTC
    I have a guess: does it work correctly if you remove the following line?
    $html = encode_entities($html);
    I suspect that the example you linked to is expecting non-html text as input and is therefore encoding all entitites, whereas your data has html tags that you want left alone.
      I tried that. I also added $html = encode_qp $html; because I think the HTML has to be quoted printable to be used in MIME. That works much better, i.e., the message gets interpreted as HTML.

      OTOH now all the quotes are disappearing from the HTML. Anyone know why that is?

Re: Mail::Sendmail Multipart MIME isn't working
by shmem (Chancellor) on Jun 28, 2008 at 23:54 UTC
    Anybody know what's going wrong here?

    Did you try to view the mail with another mail client? e.g pine, mutt, thunderbird? Looks like you don't have a "Content-Transfer-Encoding" mail header line (you have that in the parts only). Are you sure you want "quoted-printable" for your HTML part? you don't encode_qp() that.

    Q: Could you tell me the way to Tipperary?
    A: Well, I wouldn't start from here...

    I would use MIME::Lite - it does all the encoding, boundary stuff and such for you.

    sub sendmessage { my ($html,$text,$recipient,$reportdate) = @_; my $msg = MIME::Lite->new( From => 'foo@bar.org', To => $recipient, Subject => "A message from me", Type => 'multipart/alternative', Encoding => '8bit', ); $msg->attach( Data => $text, Type => 'text/plain', Encoding => 'quoted-printable', Disposition => 'inline', ); $msg->attach( Data => $html, Type => 'text/html', Encoding => 'quoted-printable', Disposition => 'inline', ); $msg->send(); }

    And you don't need here-documents... ;-)

    BTW, don't forget to either use or remove $reportdate.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-20 00:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found