キャリア別インライン画像付きHTMLメールの正しい書式
仕事でいわゆるデコメール、アレンジメール、ようするに携帯向けHTMLメールを送信するプログラムを作成することになり、いろいろ悩んだので同じことになる人がいないようにメモしておく。
docomoには仕様書があるが他のキャリアにはなかったので、ほとんど勘で作業することになった。困ったポイントは(1)auのcidは@の数と書式に制限がある(2)SoftBankはboundary文字列の長さに変な制限があること。
HTMLメールに関しては、各キャリアともガラパゴスといわれるのも仕方がない仕様だと思う。外部の画像ファイルの読み込みを制限する必要があるのは理解できるが、それでもマルチパートのメールとしての書式くらいは守ってほしいものだ。
とりあえずいくつかの端末で動作したので公開しておく。今後不具合が見つかった場合に改修するかもしれない。
改行は全てCRLFとする。 【】内はフォーマットではなく フォーマットの詳細。
docomo
MIME-Version: 1.0 FROM: me@example.com Subject: 【mime encodeしたJIS文字列】 TO: you@example.com Content-Type: multipart/related; boundary="------------boundary1" Content-Transfer-Encoding: 7bit --------------boundary1 Content-Type: multipart/alternative; boundary="------------boundary2" Content-Transfer-Encoding: 7bit --------------boundary2 Content-Type: text/plain; charset=ISO-2022-JP Content-Transfer-Encoding: 7bit 【mime encodeしたJIS文字列】 --------------boundary2 Content-Type: text/html; charset=ISO-2022-JP Content-Transfer-Encoding: quoted-printable 【quoted printableにエンコードしたJISのHTML】 (画像は<IMG src="cid:0@0">のような書式になる) --------------boundary2-- --------------boundary1 ←複数の画像がある場合はここから画像データまでを繰り返す Content-Type: image/gif; name="【mime encodeしたJIS文字列】.gif" Content-Transfer-Encoding: base64 Content-ID: <0@0> 【base64_encodeしてchunk_splitした画像データ】 --------------boundary1--
au
MIME-Version: 1.0 FROM: me@example.com Subject: 【mime encodeしたJIS文字列】 TO: you@example.com Content-Type: multipart/mixed; boundary="------------boundary1" --------------15b49b545 Content-Type: multipart/alternative; boundary="------------boundary2" --------------boundary2 Content-Type: text/plain; charset=ISO-2022-JP Content-Transfer-Encoding: 7bit 【mime encodeしたJIS文字列】 --------------boundary2 Content-Type: text/html; charset=ISO-2022-JP Content-Transfer-Encoding: quoted-printable 【quoted printableにエンコードしたJISのHTML】 (画像は<IMG src="cid:0@0">のような書式になる) --------------boundary2-- --------------boundary1 ←複数の画像がある場合はここから画像データまでを繰り返す Content-Type: image/gif; name="【mime encodeしたJIS文字列】.gif" Content-Disposition: attachment; filename="【mime encodeしたJIS文字列】.gif" Content-Transfer-Encoding: base64 Content-ID: <0@0> ←必ず「数字@数字」とする 【base64_encodeしてchunk_splitした画像データ】 --------------boundary1--
SoftBank
MIME-Version: 1.0
FROM: me@example.com
Subject: 【mime encodeしたJIS文字列】
TO: you@example.com
Content-Type: multipart/related;
boundary="------------boundary1" ←21文字にする必要がある
--------------boundary1
Content-Type: multipart/alternative;
boundary="------------boundary2" ←21文字にする必要がある
--------------boundary2
Content-Type: text/plain; charset=ISO-2022-JP
Content-Transfer-Encoding: 7bit
【mime encodeしたJIS文字列】
--------------boundary2
Content-Type: text/html; charset=ISO-2022-JP
Content-Transfer-Encoding: quoted-printable
【quoted printableにエンコードしたJISのHTML】
(画像は<IMG src="cid:0@0">のような書式になる)
--------------boundary2--
--------------boundary1 ←複数の画像がある場合はここから画像データまでを繰り返す
Content-Type: image/gif; name="【mime encodeしたJIS文字列】.gif"
Content-Disposition: inline;filename="【mime encodeしたJIS文字列】.gif"
Content-Transfer-Encoding: base64
Content-ID: <0@0>
【base64_encodeしてchunk_splitした画像データ】
--------------boundary1--
途中PHPの関数名が出てきているが、気にしない。あと、PHPでquoted printableにするのにはimap_8bit関数を使った。
Popularity: 6% [?]

Comments
[...] This post was mentioned on Twitter by twitt_bot. twitt_bot said: キャリア別インライン画像付きHTMLメールの正しい書式 | Selfkleptomaniac http://selfkleptomaniac.org/archives/1209 [...]