Perl utf8与我的脚本不太一致

时间:2011-02-16 07:16:21

标签: perl utf-8 html-email

我正在写一个关于perl脚本的utf-8支持的问题。该脚本旨在发送HTML电子邮件。 html消息以PostFSQL数据库中的UTF-8格式保存。一切似乎都在起作用,但是当我收到脚本中的电子邮件时,我仍然会受到腐败 - “ ”。

在剧本的开头我有:

#!/usr/bin/perl -w
use utf8;
use Encode;
use MIME::Base64;
use MIME::Lite;  

my $connection = DBI->connect('dbi:Pg:dbname='.$db_name.';host='.$db_host.'', $db_user,$db_pass, { AutoCommit=>1, PrintError => 1, pg_enable_utf8 => 1 });

my $fetchHtml = $connection->prepare('SELECT * FROM emails ORDER BY n_id DESC LIMIT 1');
$fetchHtml->execute();

my $message = $fetchHtml->fetchrow_hashref();

my $sendMsg = MIME::Lite->build(
    Encoding    => 'quoted-printable',
    Type        => 'multipart',
    To          => '<atesting@address.com>',
    From        => '<destination@address.com>',
    Subject     => encode("MIME-B", $message->{'title'}),
    Data        => decode_entities($message->{'html'})
);

$sendMsg->attr("Content-Type" => "text/html; charset=utf-8;");

$sendMsg->send_by_smtp('127.0.0.1', Timeout =>30, Debug => 0, SkipBad => 1);

我想知道我做错了什么,为什么我继续得到很酷的“ ”标志? :)

另一件事是我在执行脚本时遇到此异常:

Uncaught exception from user code:
    Wide character in subroutine entry at /usr/lib/perl5/site_perl/5.10.0/MIME/Lite.pm line 2259.
at /usr/lib/perl5/site_perl/5.10.0/MIME/Lite.pm line 2259
    MIME::Lite::print_simple_body('MIME::Lite=HASH(0xa51b9c8)', 'MIME::Lite::SMTP=GLOB(0xa5b8888)', 1) called at /usr/lib/perl5/site_perl/5.10.0/MIME/Lite.pm line 2191
    MIME::Lite::print_body('MIME::Lite=HASH(0xa51b9c8)', 'MIME::Lite::SMTP=GLOB(0xa5b8888)', 1) called at /usr/lib/perl5/site_perl/5.10.0/MIME/Lite.pm line 2126
    MIME::Lite::print_for_smtp('MIME::Lite=HASH(0xa51b9c8)', 'MIME::Lite::SMTP=GLOB(0xa5b8888)') called at /usr/lib/perl5/site_perl/5.10.0/MIME/Lite.pm line 2897
    MIME::Lite::send_by_smtp('MIME::Lite=HASH(0xa51b9c8)', 'bla.example.com', 'Timeout', 30, 'Debug', 0, 'SkipBad', 1) called at ./advanced-daemon.pl line 354
    main::send_mail('Subject Title' <webmaster@testing>', 'spam@spam.com', 'HASH(0xa518630)') called at ./advanced-daemon.pl line 225
    main::sendEmailsToSubscribers('DBI::db=HASH(0xa517f40)', 24, 'HASH(0xa518630)') called at ./advanced-daemon.pl line 136

我无法理解究竟是什么问题,但我认为这与utf8有关.. 任何帮助都会非常感激.. :)

1 个答案:

答案 0 :(得分:1)

首先,只有在quellcode中有unicode字符时才需要use utf8。那么decode_entities($message->{'html'})也是错的。仅使用$message->{'html'}。 默认情况下,数据库必须为utf8。然后添加Encoding => '8bit'。这对我很有用。

您的MIME :: Lite为false:请参阅http://www.perlmonks.org/?node_id=105262上的一个很好的例子