【问题标题】:Perl email script--content encoding issuePerl 电子邮件脚本——内容编码问题
【发布时间】:2012-03-04 12:40:36
【问题描述】:

我决定通过他们网站上的一些入门脚本深入研究 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 部分,但它并没有说明错误消息。我假设我需要设置编码,但我不确定如何去做。

【问题讨论】:

  • 感谢@cjm,示例现已修复,感谢您提出!

标签: perl email mime


【解决方案1】:

如果您查看SYNOPSIS section of the docs,您会发现您还可以将“属性”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 的来源是available on GitHub。我已经发送了pull request to fix this
  • 成功了!我添加了属性,然后遇到了一些额外的错误,但通过安装 sendmail 和 postfix 得到了纠正。
猜你喜欢
  • 1970-01-01
  • 2012-06-03
  • 2014-05-24
  • 1970-01-01
  • 2012-11-03
  • 1970-01-01
  • 2011-05-06
  • 2015-01-20
  • 2016-01-15
相关资源
最近更新 更多