PHPのmail()関数を使ってメールを送信します。fromフィールドをUTF-8でエンコードする必要があるため、次のメールヘッダーを設定します。
function mail_headers($from, $contentType = "text/plain") {
$headers = "From: ".mime_header_encode($from)."\n";
$headers .= 'Reply-To: '.mime_header_encode($from)."\n";
$headers .= 'MIME-Version: 1.0' ."\n";
$headers .= 'Content-Type: '.$contentType.'; charset=utf-8' ."\n";
$headers .= 'Content-Transfer-Encoding: 8bit'. "\n";
return $headers;
}
function mime_header_encode($text, $encoding = "utf-8") {
return "=?$encoding?Q?" . imap_8bit($text) . "?=";
}
私のPCではうまく機能しますが、本番サーバーではあまりうまく機能しません。エンコードされた文字列を有効な電子メールとして認識しない=?utf-8?Q?Website name <info@myshop.com>?=
ため、改善を試みます。サーバーは最終的にヘッダーのこの部分を含む電子メールを送信します。
From: =?utf-8?Q?Website name <info@myshop.com>,
?=@hosting-domain.com
このヘッダーを使用すると、電子メールの受信者には別の奇妙な送信者(コンマの後の文字列)が表示されます。PHPでメール文字列に触れないようにすることはできますか?
ありがとうございました!