デコメール送信(Perl)

マルチパートの構成

ドコモ

 multipart/mixed
 ├ multipart/related
 │├ multipart/alternative
 ││├ text/plain
 ││└ text/html
 │└ image/gif  # インライン画像
 └ image/gif    # 添付画像

AU

 multipart/mixed
 ├ multipart/alternative
 │├ text/plain
 │└ text/html
 └ image/gif
  • 添付画像は不可?

ソフトバンク

 multipart/related
 ├ multipart/alternative
 │├ text/plain
 │└ text/html
 └ image/gif   # インライン画像兼添付画像?

サンプルコード

 use utf8;
 use Encode;
 use Encode::JP::Mobile;
 use Mail::Address::MobileJp;
 use MIME::Lite;
 use HTML::Scrubber;
 use MIME::Base64;
 use Path::Class;
 
 my $email_from  = 'taro@example.net';
 my $email_to    = 'hanako@ezweb.ne.jp';
 
 my $emoji_bytes = "\xF8\x9F";  # ドコモのSJISコード「太陽」
 my $emoji_chars = decode( 'x-sjis-imode', $emoji_bytes );
 
 my $subject = "件名です";
 my $html = <<"END";
 <html>
 <body>
 <font color="#FF0000"><a href="http://portal.mobile.yahoo.co.jp/">本文です</a></font><br>
 $emoji_chars<br>
 <img src="cid:my\@image">
 </body>
 </html>
 END
 my $text = HTML::Scrubber->new->scrub($html);
 
 my $subject_encoded =
   is_imode($email_to)
   ? '=?SHIFT-JIS?B?' . MIME::Base64::encode( encode( 'x-sjis-docomo', $subject . $emoji_chars ) ) . '?='
   : is_softbank($email_to)
   ? '=?UTF-8?B?' . MIME::Base64::encode( encode( 'x-utf8-softbank', $subject . $emoji_chars ) ) . '?='
   : is_ezweb($email_to) ? encode( 'x-sjis-kddi-auto', $subject . $emoji_chars )
   :                       '=?ISO-2022-JP?B?' . MIME::Base64::encode( encode( 'iso-2022-jp', $subject ) ) . '?=';
 my $html_encoded =
     is_imode($email_to)    ? encode( 'x-sjis-docomo',    $html )
   : is_softbank($email_to) ? encode( 'x-utf8-softbank',  $html )
   : is_ezweb($email_to)    ? encode( 'x-sjis-kddi-auto', $html )
   :                          encode( 'iso-2022-jp', $html );
 my $text_encoded =
     is_imode($email_to)    ? encode( 'x-sjis-docomo',    $text )
   : is_softbank($email_to) ? encode( 'x-utf8-softbank',  $text )
   : is_ezweb($email_to)    ? encode( 'x-sjis-kddi-auto', $text )
   :                          encode( 'iso-2022-jp', $text );
 
 my $alternative = MIME::Lite->new( Type => 'multipart/alternative', );
 $alternative->attach(
     Type     => 'text/html',
     Data     => $html_encoded,
     Encoding => '8bit',
 );
 $alternative->attach(
     Type     => 'text/plain',
     Data     => $text_encoded,
     Encoding => '8bit',
 );
 my $image = MIME::Lite->new(
     Type     => 'image/gif; name="image.gif"',
     Id       => '<my@image>',
     Data     => scalar file( '.', 'image.gif' )->slurp,
     Encoding => 'base64',
 );
 if ( is_imode() ) {
     my $related = MIME::Lite->new( Type => 'multipart/related', );
     $related->attach($alternative);
     $related->attach($image);
     my $mixed = MIME::Lite->new(
         Type    => 'multipart/mixed',
         Subject => $subject_encoded,
         From    => $email_from,
         To      => $email_to,
     );
     my $image2 = MIME::Lite->new(
         Type     => 'image/gif; name="image.gif"',
         Data     => scalar file( '.', 'image.gif' )->slurp,
         Encoding => 'base64',
     );
     $mixed->attach($image2);
     $mixed->attach($related);
     $mixed->send;
 }
 elsif (is_ezweb) {
     my $mixed = MIME::Lite->new(
         Type    => 'multipart/mixed',
         Subject => $subject_encoded,
         From    => $email_from,
         To      => $email_to,
     );
     $mixed->attach($alternative);
     $mixed->attach($image);
     $mixed->send;
 }
 elsif (is_softbank || 1) {
     my $related = MIME::Lite->new(
         Type    => 'multipart/related',
         Subject => $subject_encoded,
         From    => $email_from,
         To      => $email_to,
     );
     $related->attach($alternative);
     $related->attach($image);
     $related->send;
 }
  • AUの場合、Contents-IDは"@"を1つだけ含まなければならない。2個以上含んではならない(とのこと→http://www.cpa-lab.com/tech/0135)。

参考

キャリア公式

CPAN


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS

Last-modified: 2009-09-03 (木) 08:21:14