Perl电子邮件脚本--内容编码问题

Perl电子邮件脚本--内容编码问题,perl,email,mime,Perl,Email,Mime,我决定深入perl,在他们的站点上使用一些初学者脚本。我开始尝试一个基本的电子邮件发送脚本 以下是实际代码: #!/usr/bin/perl use strict; use warnings; #Character encoding var my $encoding='text/plain; charset="iso-8859-5"'; #Create the message use Email::MIME; my $message = Email::MIME->create(

我决定深入perl,在他们的站点上使用一些初学者脚本。我开始尝试一个基本的电子邮件发送脚本

以下是实际代码:

#!/usr/bin/perl
use strict;
use warnings;

#Character encoding var
my $encoding='text/plain; charset="iso-8859-5"';

#Create the message
use Email::MIME;
my $message = Email::MIME->create(
        header_str => [
                From => 'gmail@gmail.com',
                To => 'gmail2@gmail.com',
                Subject => 'I sent you this message using Perl.',
        ],
        body_str => 'I sent you this message using Perl.  One of those languages that would\' would\'ve been helpful much sooner in my life...',
        );
use Email::Sender::Simple qw(sendmail);
sendmail($message);
当我执行
perlscript.pl
时得到的是

body_str was given, but no charset is defined at /usr/local/share/perl/5.10.1/Email/MIME.pm line 243
    Email::MIME::create('Email::MIME', 'header_str', 'ARRAY(0x9a04818)', 'body_str', 'I sent you this message using Perl.  One ...') called at script.pl line 10
我搜索了Email::MIME模块,找到了body_str部分,但没有发现任何错误消息。我假设我需要设置编码,但我不确定如何进行设置。

如果您查看了,您将看到还可以传递“attributes”hashref以创建()。您可以在这里定义字符集。您可能还会发现,您还需要在这里定义编码。例如,您可以执行以下操作:

my $message = Email::MIME->create(
    header_str => [
            From    => 'gmail@gmail.com',
            To      => 'gmail2@gmail.com',
            Subject => 'I sent you this message using Perl.',
    ],
    attributes => {
        encoding => 'quoted-printable', 
        charset  => "US-ASCII",
    },
    body_str => 'I sent you this message using Perl.  One of those languages that would\' would\'ve been helpful much sooner in my life...',
);
如果查看,您将看到还可以将“attributes”hashref传递给create()。您可以在这里定义字符集。您可能还会发现,您还需要在这里定义编码。例如,您可以执行以下操作:

my $message = Email::MIME->create(
    header_str => [
            From    => 'gmail@gmail.com',
            To      => 'gmail2@gmail.com',
            Subject => 'I sent you this message using Perl.',
    ],
    attributes => {
        encoding => 'quoted-printable', 
        charset  => "US-ASCII",
    },
    body_str => 'I sent you this message using Perl.  One of those languages that would\' would\'ve been helpful much sooner in my life...',
);

顺便说一句,learn.perl.org的源代码是。我已经发了一封信,这就成功了!我添加了属性,然后遇到了一些额外的错误,但通过安装sendmail和postfix.BTW(learn.perl.org的源代码为)进行了修复。我已经发了一封信,这就成功了!我添加了属性,然后遇到了一些额外的错误,但通过安装sendmail和postfix进行了修复。由于@cjm,该示例现在已经修复,感谢您提出它!多亏了@cjm,这个例子现在已经被修复了,谢谢你提出它!