Perl utf8 不太适合我的脚本

发布于 2024-10-17 09:04:41 字数 2725 浏览 4 评论 0原文

我在编写的 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          => '<[email protected]>',
    From        => '<[email protected]>',
    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>', '[email protected]', '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 有关。 任何帮助将非常感激..:)

I'm having a problem with utf-8 support on a perl script I'm writing. The script is meant to send html email messages. The html messages are saved in UTF-8 format inside a PostgreSQL database. Everything seems to be working but still I get corruption sometimes when I receive an email from the script - "�".

In the beginning of the script I have:

#!/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          => '<[email protected]>',
    From        => '<[email protected]>',
    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);

I'm wondering what I'm doing wrong and why do I keep on getting the cool "�" sign ? :)

Another thing is that I get this exception when I execute the script:

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>', '[email protected]', '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

I can't understand what exactly is the problem but I think it's related to the utf8..
Any help would be pretty much appreciated.. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等待圉鍢 2024-10-24 09:04:41

首先,仅当 quellcode 中包含 unicode 字符时,才需要使用 utf8。那么 decode_entities($message->{'html'}) 也是错误的。仅使用 $message->{'html'}
数据库默认必须是utf8。然后添加 Encoding => “8 位”。这对我来说很好用。

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

First you need use utf8 only if you have unicode character in the quellcode. Then decode_entities($message->{'html'}) ist also wrong. Use only $message->{'html'}.
The database must be utf8 by default. Then add Encoding => '8bit'. That works nice for me.

Your MIME::Lite is false: see on http://www.perlmonks.org/?node_id=105262 for a nice example

伴梦长久 2024-10-24 09:04:41

在我的代码中,我使用 multipart 类型,这是似乎可以正常工作的版本。

my $mail_htm = 'Text of the email with utf8 characters like these: ľščťžýáíúäô';
my $msg = MIME::Lite->new(
    From      =>'[email protected]',
    'Reply-To'=>'[email protected]',
    To        =>'[email protected]',
    Subject   => 'Simple ascii non-utf8 subject',
    Type      =>'multipart/mixed'
);
my $msg_body = MIME::Lite->new(
    Type    =>'multipart/alternative'
);
$msg_body->attach(
    Type    =>'text/html; charset=UTF-8',
    Encoding=>'quoted-printable',
    Data    =>$mail_htm
);
$msg->attach($msg_body);

对于这个主题,我没有太多使用 utf8 编码,因为我不需要它太多,而且我发现的几个解决方案都不起作用。

In my code I use the multipart type and here is the version which seems to work correctly.

my $mail_htm = 'Text of the email with utf8 characters like these: ľščťžýáíúäô';
my $msg = MIME::Lite->new(
    From      =>'[email protected]',
    'Reply-To'=>'[email protected]',
    To        =>'[email protected]',
    Subject   => 'Simple ascii non-utf8 subject',
    Type      =>'multipart/mixed'
);
my $msg_body = MIME::Lite->new(
    Type    =>'multipart/alternative'
);
$msg_body->attach(
    Type    =>'text/html; charset=UTF-8',
    Encoding=>'quoted-printable',
    Data    =>$mail_htm
);
$msg->attach($msg_body);

I didn't play much with utf8 encoding for the subject as I didn't need it that much and several solutions I found around didn't work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文