SoftBank宛に絵文字の入ったメールをPHPから送ってみる

こうする! 文字コードShift_JIS限定のよう。

<?php
// 絵文字の変換表 (誰か作って)
$emo = array(
    'heart' => pack('H*', 'F962'),
    //...
);

// ここに書く
$subject = emoemo('やわらか銀行<heart>←ここがハートの絵文字になる予定', $emo);
$body = emoemo('...', $emo);

// エンコードするよ
$subject = '=?shift_jis?B?' . base64_encode($subject) . '?=';
$body = base64_encode($body);

// ヘッダを作る
$headers = "From: $from
MIME-Version: 1.0
Content-Type: text/plain; charset=Shift-JIS
Content-Transfer-Encoding: base64";

// メールを送る
mail($to, $subject, $body, $headers);


// 絵文字を変換する関数
function emoemo($s, &$emo, $encoding = 'Shift_JIS')
{
    $r = array();
    $a = explode('<', $s);
    $r[] = mb_convert_encoding($a[0], $encoding);
    for ($i = 1; $i < count($a); $i++) {
        if (preg_match('/^(.+?)>/', $a[$i], $m) && isset($emo[$m[1]])) {
            $e = $emo[$m[1]];
            $r[] = $e . mb_convert_encoding(
                preg_replace('/^(.+?)>/', '', $a[$i]), $encoding);
        }
        else {
            $r[] = '<' . mb_convert_encoding($a[$i], $encoding);
        }
    }
    return implode('', $r);
}

参考

さらに参考