PHP mail関数を使って複数のファイルを添付する

PHP・MySQL
| | コメント(0) | トラックバック(0)

PHPのmail関数を使って、複数のファイルを添付してメールを送信する方法を紹介します。

// メールヘッダ部
$header="From:".【メール送信元】."\n".
       "MIME-Version: 1.0\n".
       "Content-Type: multipart/mixed;".
       " boundary=\"__BOUNDARY__\"\n\n".
       "Mime-Version: 1.0\n".
       "Content-Type: text/plain; charset=ISO-2022-JP\n".
       "Content-Transfer-Encoding: 7bit";

// メール本文部
$mail_body="メール本文\n".
"--__BOUNDARY__\n";

// 添付ファイル部
$temp_file_body ="";

// 添付ファイル1
$file1_name="【添付ファイルパス】";
$file = fopen($file1_name,"r");
$file1_contents=file_get_contents($file1_name);
fclose($file);
// 添付ファイル2
$file2_name="【添付ファイルパス】";
$file = fopen($file2_name,"r");
$file2_contents=file_get_contents($file2_name);
fclose($file);
// 添付ファイル3
$file3_name="【添付ファイルパス】";
$file = fopen($file3_name,"r");
$file3_contents=file_get_contents($file3_name);
fclose($file);

// 添付ファイル1の記述
$temp_file_body .="Content-Type: application/octet-stream;name=\"【添付ファイル1名】\";\n";
$temp_file_body .= "Content-Disposition: attachment; filename=\"【添付ファイル1名】\";\n";
$temp_file_body .= "Content-Transfer-Encoding: base64\n\n";
$temp_file_body .= chunk_split(base64_encode($file1_contents)) . "\n";

// 区切り
$temp_file_body .= "--__BOUNDARY__\n";

// 添付ファイル2の記述
$temp_file_body .="Content-Type: application/octet-stream;name=\"【添付ファイル1名】\";\n";
$temp_file_body .= "Content-Disposition: attachment; filename=\"【添付ファイル1名】\";\n";
$temp_file_body .= "Content-Transfer-Encoding: base64\n\n";
$temp_file_body .= chunk_split(base64_encode($file2_contents)) . "\n";

// 区切り
$temp_file_body .= "--__BOUNDARY__\n";

// 添付ファイル3の記述
$temp_file_body .="Content-Type: application/octet-stream;name=\"【添付ファイル1名】\";\n";
$temp_file_body .= "Content-Disposition: attachment; filename=\"【添付ファイル1名】\";\n";
$temp_file_body .= "Content-Transfer-Encoding: base64\n\n";
$temp_file_body .= chunk_split(base64_encode($file3_contents)) . "\n";

// 結合
$send_mail_body=$header.$mail_body.$temp_file_body;

// 文字エンコードをJISに変換
$send_mail_body=mb_convert_encoding($send_mail_body,'JIS');

// 文字エンコードをJISに変換
$subject=mb_convert_encoding("メール件名",'JIS');

// メール送信
mail(【メール宛先】,$subject,$send_mail_body,$header);


【WEB開発】カテゴリ:


【WEB開発】トラックバック(0)

【WEB開発】このブログ記事を参照しているブログ一覧: PHP mail関数を使って複数のファイルを添付する

【WEB開発】このブログ記事に対するトラックバックURL: http://web.creator-world.net/MT/mt-tb.cgi/88



【WEB開発】コメントする



カスタム検索

ioPLAZA【アイ・オー・データ直販サイト】 ioPLAZA【アイ・オー・データ直販サイト】
あれもこれも標準装備のレンタルサーバ あれもこれも標準装備のレンタルサーバ


Web広告限定ストア(eクーポン)Web広告限定ストア(eクーポン)

問い合わせ

メールフォーム