【问题标题】:Perl utf8 not quite working with my scriptPerl utf8 不太适合我的脚本
【发布时间】:2011-06-28 03:59:19
【问题描述】:

我在编写的 perl 脚本上遇到了 utf-8 支持问题。该脚本旨在发送 html 电子邮件。 html 消息以 UTF-8 格式保存在 PostgreSQL 数据库中。一切似乎都在工作,但有时当我收到来自脚本的电子邮件 - “�”时,我仍然会受到损坏。

在脚本的开头我有:

#!/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.. 任何帮助将不胜感激.. :)

【问题讨论】:

  • 您能说得更具体些吗?你得到的只是这个标志还是其他格式正确的字符?你知道它应该是哪个字符吗?
  • 不,我不知道它应该是什么字符。 html是通过php生成的,它是随机的。我确信插入数据库的数据是 utf8 编码的。为什么这个标志会显示 - 我不知道......当我通过 php 打印出 html 时,我没有显示......更有趣的是,偶尔会出现一次异常。我如何调查它为什么抛出和异常?

标签: perl utf-8 html-email


【解决方案1】:

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

您的 MIME::Lite 是错误的:请参阅 http://www.perlmonks.org/?node_id=105262 以获得一个很好的示例

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 2021-11-28
    • 1970-01-01
    • 2016-07-31
    • 2018-11-05
    • 2020-12-22
    相关资源
    最近更新 更多