2012年4月17日 星期二

奇科電腦 Perl Mail::Sendmail 傳送 utf8字元

<更多技術小秘訣請看 http://www.geego.com.tw/tech_support/new-tips>

當看到錯誤訊息Wide character in subroutine entry at /usr/share/perl5/Mail/Sendmail.pm line 237.
原因可能為使用perl的Mail::Sendmail模組寄送中文時會出現的問題,請使用 use Encode把內容編碼後,
再寄信時把信件表頭中加入 Content-type = 'text/plain; charset="utf-8"' 即可

程式範例:

#!/usr/bin/perl

use Mail::Sendmail;

use utf8;
use Encode;


$email = 'perl.mail::sendmail@geego.com.tw';
$course = "test course";

my $message = "這是一封用來測試perl的Mail::Sendmail模組可否成功寄送utf8字元.\n";

$message = encode( "utf8", $message );

my %mail = ( To => "$email",
From => 'GeeGo Customer Service<customers@geego.com.tw>',
'Content-type' => 'text/plain; charset="utf-8"',
Subject => "Perl’s Mail::Sendmail sending text body with utf8 encoding",
Message => "$message" );

sendmail(%mail) or die $Mail::Sendmail::error;

<更多技術小秘訣請看 http://www.geego.com.tw/tech_support/new-tips>

沒有留言:

張貼留言