2012-02-13 91 views
1

我決定在他們的網站上用一些初學者腳本深入Perl。我從http://learn.perl.org/examples/email.html開始嘗試一個基本的電子郵件發送腳本。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 => '[email protected]', 
       To => '[email protected]', 
       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是我做的電子郵件:: MIME模塊周圍一些搜索,發現body_str的

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 

的錯誤信息部分,但沒有揭示錯誤信息。我假設我需要設置編碼,但我不知道如何去做。

+0

感謝@cjm這個例子現在已經修復了,感謝你提出這個例子! – 2012-02-13 06:17:10

回答

5

如果你看看SYNOPSIS section of the docs,你會發現你也可以傳遞一個「attributes」hashref到create()。你可以在這裏定義字符集。你也可能發現你也需要在這裏定義編碼。例如,你可以這樣做:

my $message = Email::MIME->create(
    header_str => [ 
      From => '[email protected]', 
      To  => '[email protected]', 
      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...', 
); 
+0

BTW,learn.perl.org的源代碼[可在GitHub上獲得](https://github.com/perlorg/perlweb)。我發送了一個[pull request to fix this](https://github.com/perlorg/perlweb/pull/35)。 – cjm 2012-02-13 05:45:21

+0

這個伎倆!我添加了屬性,然後遇到了一些額外的錯誤,但它通過安裝sendmail和postfix來解決。 – Chad 2012-02-13 17:13:54

+0

@cwscribner太好了!如果照顧它,您可以將其標記爲接受的答案。 :) – oalders 2012-02-13 19:44:27