Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

MIME::Lite how to avoid wide character in subroutine for quoted/printable?

by PerlBroker (Acolyte)
on Mar 23, 2010 at 20:55 UTC ( [id://830401]=perlquestion: print w/replies, xml ) Need Help??

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

I work under strict and warnings. And I get this error: Wide character in subroutine entry at /usr/local/share/perl/5.10.0/MIME/Lite.pm line 2259., referer: Although I check for all arguments to be UTF-8, and they are utf8::valid, the error appears again and again. The $message does not arrive, but $name, $subject work fine. $message = 'ćžćššš' (UTF-8) and it comes from CGI.pm input. Anyone knows how to avoid correctly wide character in print in this case? This is the code:
sub message { my ($email, $from, $name, $message, $domain, $referer) = @_; $message .= qq{\n\n---\nYour free service at http://example.com\n} +; utf8::decode($message); utf8::upgrade($message); utf8::decode($name); utf8::upgrade($name); $from = "=?UTF-8?B?" . encode_base64(encode("utf8", qq{"$name"}), +"") . "?= <$from>"; my $subject = "New message from $referer"; $subject = "=?UTF-8?B?" . encode_base64(encode("utf8", qq{$subject +}), "") . "?="; utf8::decode($subject); utf8::upgrade($subject); my $msg = MIME::Lite->new( From => $from, To => "$email", Subject => $subject, Type => 'multipart/related' ); $msg->attach( Type => 'text/plain; charset=UTF-8', Data => $message, Encoding => "quoted-printable" ); $msg->attr("content-type.charset" => 'UTF8'); $msg->send; page("Message sent. Thank you", qq{<p>Thank you very much. Your me +ssage has been sent.<br/><br/>Go back to: <a href="$referer">$referer +</a>}); }
  • Comment on MIME::Lite how to avoid wide character in subroutine for quoted/printable?
  • Download Code

Replies are listed 'Best First'.
Re: MIME::Lite how to avoid wide character in subroutine for quoted/printable?
by ikegami (Patriarch) on Mar 23, 2010 at 21:30 UTC
    The documentation isn't clear on whether it expects text or bytes (i.e. encoded text) for each header and for messages. The following basic code gives the warning, indicating it wants the text to be encoded for at least some of them:
    #!/usr/bin/perl -w use strict; use warnings; use MIME::Lite qw( ); my $addr = '...@....com'; sub message { my ($to_name, $to_addr, $fr_name, $fr_addr, $subject, $body) = @_; my $msg = MIME::Lite->new( From => qq{"$fr_name" <$fr_addr>}, To => qq{"$to_name" <$to_addr>}, Subject => $subject, Type => 'multipart/related', ); $msg->attach( Type => 'text/plain; charset=UTF-8', Data => $body, Encoding => 'quoted-printable', ); $msg->send; } message( # Control 'a', $addr, 'a', $addr, 'a', 'a', ); message( # Test "\x{2660}", $addr, "\x{2660}", $addr, "\x{2660}", "\x{2660}", );

    The documentation says it handle MIME encoding already, so all we should have to do is the character encoding.

    sub message { my ($to_name, $to_addr, $fr_name, $fr_addr, $subject, $body) = @_; my $fr = qq{"$fr_name" <$fr_addr>}; my $to = qq{"$to_name" <$to_addr>}; utf8::encode( $_ ) for $to, $fr, $subject, $body; my $msg = MIME::Lite->new( From => $fr, To => $to, Subject => $subject, Type => 'multipart/related', ); $msg->attach( Type => 'text/plain; charset=UTF-8', Data => $body, Encoding => 'quoted-printable', ); $msg->send; }

    The warning is gone, and the message displays fine, but looking at the raw email shows that the From, To and Subject fields aren't MIME-encoded. I guess we'll have to do that too.

    sub message { my ($to_name, $to_addr, $fr_name, $fr_addr, $subject, $body) = @_; $_ = encode('MIME-Header', $_) for $to_name, $fr_name, $subject; utf8::encode( $body ); my $msg = MIME::Lite->new( From => qq{"$fr_name" <$fr_addr>}, To => qq{"$to_name" <$to_addr>}, Subject => $subject, Type => 'multipart/related', ); $msg->attach( Type => 'text/plain; charset=UTF-8', Data => $body, Encoding => 'quoted-printable', ); $msg->send; }

    And bingo!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-24 11:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found