我决定在他们的网站上使用一些入门脚本深入了解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);
Run Code Online (Sandbox Code Playgroud)
我做的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
Run Code Online (Sandbox Code Playgroud)
我做了一些搜索Email :: MIME模块并找到了body_str部分,但它没有说明错误消息.我假设我需要设置编码,但我不知道如何去做.
如果你看一下docs的SYNOPSIS部分,你会发现你也可以将一个"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...',
);
Run Code Online (Sandbox Code Playgroud)