dehio3’s diary

仕事、生活、趣味のメモ

メール送信時の文字コードについて

目的

メール送信処理してるperlスクリプトについて、perlのバージョンアップ&OSの文字コード変更に伴い改修する。

環境

変更前

$ cat /etc/sysconfig/i18n
LANG="ja_JP.eucJP"
SUPPORTED="ja_JP.eucJP:ja_JP:ja"
SYSFONT="latarcyrheb-sun16"
$ perl -version

This is perl, v5.8.5 built for x86_64-linux-thread-multi

Copyright 1987-2004, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'.  If you have access to the
Internet, point your browser at http://www.perl.com/, the Perl Home Page.

変更後

# cat /etc/sysconfig/i18n
LANG=ja_JP.UTF-8
# perl -version

This is perl 5, version 16, subversion 3 (v5.16.3) built for x86_64-linux-thread-multi
(with 29 registered patches, see perl -V for more detail)

Copyright 1987-2012, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

処理

修正が必要な処理だけ抜粋

use Jcode;
require './mimew.pl';


(中略)

### サブジェクトを jis にして、MIME エンコード
$subject = mimeencode(Jcode'jis($subject));

### 本文を jis に
$body = Jcode'jis($body);

### 添付するデータを、base64 でエンコード
$attach = &bodyencode($attach_tmp, "b64");
$attach .= &benflush("b64");

### ファイル名を sjis にして MIME エンコード。(推奨 ascii )
$filename = mimeencode(Jcode'sjis($filename));


### 全体のヘッダ
print MAIL "MIME-Version: 1.0\n";
print MAIL "Content-Type: Multipart/Mixed; boundary=\"$boundary\"\n";
print MAIL "Content-Transfer-Encoding:Base64\n";
print MAIL "From: $from\n";
print MAIL "To: $to\n";
#print MAIL "cc: $cc\n";
print MAIL "Subject: $subject\n";

### メール本文のパート
print MAIL "--$boundary\n";
print MAIL "Content-Type: text/plain; charset=\"ISO-2022-JP\"\n";
print MAIL "\n";
print MAIL "$body\n";

### 添付ファイルのパート
print MAIL "--$boundary\n";
print MAIL "Content-Type: application/octet-stream; name=\"$filename\"\n";
print MAIL "Content-Transfer-Encoding: base64\n";
print MAIL "Content-Disposition: attachment; filename=\"$filename\"\n";
print MAIL "\n";
print MAIL "$attach\n";
print MAIL "\n";

### マルチパートのおわり。
print MAIL "--$boundary" . "--\n";

調査

そもそものメール送信作法

参考 MIME の基礎

MIMEへの変換

 ### サブジェクトを jis にして、MIME エンコード
-$subject = mimeencode(Jcode'jis($subject));
+$subject = encode('MIME-Header', $subject);
 ### ファイル名を sjis にして MIME エンコード。(推奨 ascii )
-$filename = mimeencode(Jcode'sjis($filename));
+$filename = encode('MIME-Header', encode('sjis', $filename));

JISへの変換

 ### 本文を jis に
-$body = Jcode'jis($body);
+$body = encode('jis', $body);

base64への変換

 ### 添付するデータを、base64 でエンコード
-$attach = &bodyencode($attach_tmp, "b64");
-$attach .= &benflush("b64");
+$attach = encode_base64($attach_tmp);