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

时间:2012-02-13 02:51:50

标签: perl email mime

我决定使用他们网站上的一些入门脚本深入了解perl。我从http://learn.perl.org/examples/email.html开始尝试使用基本的电子邮件发送脚本。

这是实际的代码:

#!/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);

当我perl script.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部分,但它没有说明错误信息。我假设我需要设置编码,但我不确定如何去做。

1 个答案:

答案 0 :(得分:5)

如果您查看SYNOPSIS section of the docs,您会看到您还可以将“attributes”hashref传递给create()。你可以在这里定义charset。您可能还会发现您还需要在此处定义编码。例如,你可以这样做:

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...',
);