22
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPでの日本語対応メール+添付ファイル

Last updated at Posted at 2017-12-03

#はじめに
 ファイルを添付した日本語のメールを送る必要があったので、送り方のまとめです。
 PHPは7.1、ソースファイルはUTF-8Nで動作確認済みです。
#メールを送る
 メールタイトルと本文のみ日本語を使うなら、単純に下のようにすれば送れます。

$to = "to@xxxx.co.jp";
$from = "from@xxxx.co.jp";
$subject = "日本語タイトル";
$body = "日本語の入った本文\n簡単ですね。";

mb_language("ja");
mb_internal_encoding('utf-8');
$head  = "From: {$from} \n";
if (!mb_send_mail($to, $subject, $body, $head)) {
	error_log('mb_send_mail fail');
}

#Fromの日本語対応
差出人を from@xxxx.co.jp と表示させるのではなく、差出人<from@xxxx.co.jp> と表示させるには以下の様にします。

$to = "to@xxxx.co.jp";
$from = "from@xxxx.co.jp";
$fromtitle = "差出人";
$subject = "日本語タイトル";
$body = "日本語の入った本文\n簡単ですね。";

mb_language("ja");
mb_internal_encoding('utf-8');
$head  = "From: " . mb_encode_mimeheader(mb_convert_encoding($from_name,"ISO-2022-JP")) . "<{$from}> \n";
if (!mb_send_mail($to, $subject, $body, $head)) {
	error_log('mb_send_mail fail');
}

#toの日本語対応
ついでに宛先も to@xxxx.co.jp と表示させるのではなく、差出人<to@xxxx.co.jp> と表示させるには以下の様にします。

$to = "to@xxxx.co.jp";
$totitle = "宛先";
$from = "from@xxxx.co.jp";
$fromtitle = "差出人";
$subject = "日本語タイトル";
$body = "日本語の入った本文\n簡単ですね。";

mb_language("ja");
mb_internal_encoding('utf-8');
$to = mb_encode_mimeheader(mb_convert_encoding($totitle,"ISO-2022-JP")) . "<{$to}>";
$head  = "From: " . mb_encode_mimeheader(mb_convert_encoding($from_name,"ISO-2022-JP")) . "<{$from}> \n";
if (!mb_send_mail($to, $subject, $body, $head)) {
	error_log('mb_send_mail fail');
}

複数宛先に送るには

$to = mb_encode_mimeheader(mb_convert_encoding("宛先1","ISO-2022-JP")) . "<{$to1}>";
$to .= "," . mb_encode_mimeheader(mb_convert_encoding("宛先2","ISO-2022-JP")) . "<{$to2}>";

の様にします。

#添付ファイルを付けるには
 添付ファイルのファイル名が $filename、ファイル本体が $filebody に入っているとすると。

$to = "to@xxxx.co.jp";
$totitle = "宛先";
$from = "from@xxxx.co.jp";
$fromtitle = "差出人";
$subject = "日本語タイトル";
$message = "日本語の入った本文\n簡単ですね。";
$filename = "日本語ファイル.pdf";
$filebody = file_get_contents("tmp.pdf");

mb_language("ja");
mb_internal_encoding('utf-8');
$mime_type = "application/octet-stream";
$boundary = '----=_Boundary_' . uniqid(rand(1000,9999) . '_') . '_';
$to = mb_encode_mimeheader(mb_convert_encoding($totitle,"ISO-2022-JP")) . "<" . $to . ">";
$head  = "From: " . mb_encode_mimeheader(mb_convert_encoding($from_name,"ISO-2022-JP")) . "<" . $from . "> \n";
$head .= "MIME-Version: 1.0\n";
$head .= "Content-Type: multipart/mixed; boundary=\"{$boundary}\"\n";
$head .= "Content-Transfer-Encoding: 7bit";

// 本文
$body .= "--{$boundary}\n";
$body .= "Content-Type: text/plain; charset=ISO-2022-JP;" .
	"Content-Transfer-Encoding: 7bit\n";
$body .= "\n";
$body .= "{$message}\n";
$body .= "\n";
// 添付ファイル
$filename = mb_convert_encoding($filename, 'ISO-2022-JP');
$filename = "=?ISO-2022-JP?B?" . base64_encode($filename) . "?=";
$body .= "--{$boundary}\n";
$body .= "Content-Type: {$mime_type}; name=\"{$filename}\"\n" .
	"Content-Transfer-Encoding: base64\n" .
	"Content-Disposition: attachment; filename=\"{$filename}\"\n\n";
$f_encoded = chunk_split(base64_encode($filebody));
$body .= $f_encoded . "\n\n";

if (!mb_send_mail($to, $subject, $body, $head)) {
	error_log('mb_send_mail fail');
}

簡単ですね。

22
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?