PCからデコメールを送るときの仕様

[参考記事] PHPでHTMLメールを送る方法
[参考記事] mb_send_mailでCCやBCCを指定する 表示名を指定する
[参考記事] mail関数やmb_send_mail関数でReturn-Pathを設定する方法
[参考記事] mailto本文での改行 ドコモのN、Pで送信に失敗します
[参考記事] mailtoの使い方
[参考記事] 携帯サイトでのmailtoの使い方

HTMLソース

HTMLソースは3キャリア共通

<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=iso-2022-jp">
</HEAD>
<BODY>
本文
</BODY>
</HTML>

<IMG>タグはContent-IDで置き換える。

Docomo、auの<IMG>タグ置き換え例

<IMG src="cid:02@【任意文字】">

Softbankの<IMG>タグ置き換え例

<IMG src="cid:01.【任意文字】">

Docomo

Subject: =?iso-2022-jp?B?【タイトル[mimeheader]】=?=
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="【boundary1】"
Content-Transfer-Encoding: 7bit

--【boundary1】
Content-Type: multipart/related; boundary="【boundary2】"

--【boundary2】
Content-Type: multipart/alternative; boundary="【boundary3】"

--【boundary3】
Content-Type: text/plain; charset="iso-2022-jp"
Content-Transfer-Encoding: 7bit

【HTMLメールが表示できなかったときの本文】
--【boundary3】
Content-Type: text/html; charset="iso-2022-jp"
Content-Transfer-Encoding: quoted-printable

【HTMLソースをquoted-printableエンコードした本文】
--【boundary3】--

--【boundary2】
Content-Type: 【mimeタイプ】;
 name="【画像ファイル名】"
Content-Transfer-Encoding: base64
Content-ID: <01@【任意文字】>

【base64エンコードし76文字で改行した画像データ】
--【boundary2】
Content-Type: 【mimeタイプ】;
 name="【画像ファイル名】"
Content-Transfer-Encoding: base64
Content-ID: <02@【任意文字】>

【base64エンコードし76文字で改行した画像データ】
--【boundary2】--
--【boundary1】--

au

Subject: =?iso-2022-jp?B?【タイトル[mimeheader]】=?=
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="【boundary1】"


--【boundary1】
Content-Type: multipart/alternative; boundary="【boundary2】"


--【boundary2】
Content-Type: text/plain; charset="iso-2022-jp"
Content-Transfer-Encoding: 7bit

【HTMLメールが表示できなかったときの本文】

--【boundary2】
Content-Type: text/html; charset="iso-2022-jp"
Content-Transfer-Encoding: quoted-printable

【HTMLソースをquoted-printableエンコードした本文】
--【boundary2】--

--【boundary1】
Content-Type: 【mimeタイプ】; name="【画像ファイル名】"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="【画像ファイル名】"
Content-ID: <01@【任意文字】>

【base64エンコードし76文字で改行した画像データ】

--【boundary1】
Content-Type: 【mimeタイプ】; name="【画像ファイル名】"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="【画像ファイル名】"
Content-ID: <02@【任意文字】>

【base64エンコードし76文字で改行した画像データ】

--【boundary1】--

Softbank

Subject: =?iso-2022-jp?B?【タイトル[mimeheader]】=?=
Mime-Version: 1.0
Content-Type:multipart/related;
	boundary="【boundary1】"
From: <【送り元メールアドレス】>
To: 【送り先メールアドレス】
Sender:【送り元メールアドレス】
X-Priority: 【重要度(1〜5)】


--【boundary1】
Content-Type:multipart/alternative;
	boundary="【boundary2】"


--【boundary2】
Content-Type:text/plain;charset=ISO-2022-JP
Content-Transfer-Encoding:7bit

【HTMLメールが表示できなかったときの本文】
--【boundary2】
Content-Type:text/html;charset=ISO-2022-JP
Content-Transfer-Encoding:quoted-printable

【HTMLソースをquoted-printableエンコードした本文】
--【boundary2】--

--【boundary1】
Content-Type:【mimeタイプ】;name="=?ISO-2022-JP?B?【ファイル名[mimeheader]】?="
Content-Disposition:inline;
 filename="=?ISO-2022-JP?B?【ファイル名[mimeheader]】?="
Content-Transfer-Encoding:base64
Content-ID:<01.【任意文字】>

【base64エンコードし76文字で改行した画像データ】

--【boundary1】
Content-Type:【mimeタイプ】;name="=?ISO-2022-JP?B?【ファイル名[mimeheader]】?="
Content-Disposition:inline;
 filename="=?ISO-2022-JP?B?【ファイル名[mimeheader]】?="
Content-Transfer-Encoding:base64
Content-ID:<02.【任意文字】>

【base64エンコードし76文字で改行した画像データ】

--【boundary1】--

デコメールの送信に使えるPHPコード

mail関数を使う

改行コードにCR+LF(\r\n)は使えません。必ずLF(\n)とします。

boundary値を作る

$boundary = md5(uniqid());

もっと複雑な値を得るなら

$boundary = md5(uniqid(rand(), true));

base64へエンコード

mb_language("japanese");
$subject = mb_encode_mimeheader($subject);

または

mb_language("ja");
$subject = mb_encode_mimeheader($subject);

mb_languageを指定しないと、ISO-2022-JPでの変換がされないことがあります。
またこの関数に渡す値はISO-2022-JPへ変換しては駄目です。

=?UTF-8?B?44OG44K544OI44Gq44Oh44O844Or44KS5pu444GE44Gm44G/44KL?=

ファイルの読み込み

if(!file_exists("【ファイルのパス】")) {
  echo "読み込みファイルがありません";
  exit;
}
$source="";
if(!(@$fp = fopen("【ファイルのパス】","r"))) {
  echo "読み込みエラー";
  exit;
}else{
  while(!feof($fp)) {
    $source .= fgets($fp,4096);
  }
  fclose($fp);
}

ファイルハンドラが開けなかったときに、そのままwhile(!feof($fp))をしてしまうと、無限ループになってしまう。
このため読み込めたときのみwhileに渡すようにします。

HTMLタグを取り除く

$source = strip_tags($source);

iso-2022-jpに変換

$source = mb_convert_encoding($source,"iso-2022-jp","SJIS-win"); //Shift-JIS
$source = mb_convert_encoding($source,"iso-2022-jp","EUCJP-win");//EUC-JP
$source = mb_convert_encoding($source,"iso-2022-jp","UTF-8");    //UTF-8

Shift-JISやEUC-JPを使うときには、SJIS-winやEUCJP-winを指定するようにします。

base64エンコードし76文字で改行する

$contents = chunk_split(base64_encode($contents));

quoted-printableエンコード

$html = quoted_printable_encode($html);

quoted-printableエンコードが使えない場合&仕様

Docomoでは下記の変換でも問題はないが、Softbankではこれは使えない。

$html = mb_convert_encoding($html,"quoted-printable","iso-2022-jp");

HTMLソース内の<IMG>タグを置き換える

$imgs=array();
$imgid=time();
$strDir=opendir($mailpartsdir);
$i=0;
if($carrier==3){
  $imgdeli=".";
}else{
  $imgdeli="@";
}
while($str=readdir($strDir)){
  if($str=="." || $str=="..")continue;
  if(preg_match("/\.(gif|jpg|jpeg)$/",$str,$match)){
    $i++;
    if($match[1]=="gif"){
      $type="image/gif";
    }else{
      $type="image/jpeg";
    }
    $imgs[]=array(
      "name"=>$str,
      "type"=>$type,
      "cid"=>sprintf("%02d",$i).$imgdeli.$imgid.$i,
      "use"=>false
    );
  }
}
closedir($strDir);

function img_replace($str){
  global $imgs,$nonimg;
  if(preg_match("/src=\"([^\"]+)\"/i",$str,$match) ||
     preg_match("/src='([^']+)'/i",$str,$match)){
    foreach($imgs as $key=>$value){
      if($value["name"]==$match[1]){
        $imgs[$key]["use"]=true;
        return "<IMG src=\"cid:".$value["cid"]."\">";
      }
    }
  }
  return $str;
}

$source = preg_replace_callback("/<img [^>]+>/is",
                                create_function(
                                    '$matches',
                                    'return img_replace($matches[0]);'
                                ),
                                $source
                                );

関連記事

スポンサーリンク

ALTER TABLE テーブルの属性を変更する

ホームページ製作・web系アプリ系の製作案件募集中です。

上に戻る