PHPのファイル添付機能付きメールフォームについて(2) - PHPプロ!Q&A掲示板
2689
- 1000P
- 1000P
PHPのファイル添付機能付きメールフォームについて(2)
質問日時 / 2010年4月21日 19:23 回答数 / 13件
Questioner: warashi4
Tweet
キーワード / キーワードが設定されていません
以下ページの続きですが宜しくお願いいたします。
http://www.phppro.jp/qa/2688
PHP5.1.6では、以下ソースで問題なく送信できました。
ですが、PHP4.2.2の方では、何度やっても”送信できませんでした。”となります。
エラーが出るわけでも無いため、困り果てています。
ソースは動作確認後にもちろん修正しますが、何とか動作確認を取りたいのです。
何が悪いのか、心当たりがありましたら教えていただけないでしょうか?
- $mail['to']['name'] = '○○様';
- $mail['to']['mail'] = '00@***.com';
- $mail['from']['name'] = '××';
- $mail['from']['mail'] = '11@***.com';
- $subject = '添付ファイルテスト';
- $message = '添付ファイルテスト';
- $filename = '01.pdf';
- $attach_file = '01.pdf'; // 添付ファイル へのパス
- $mime_type = "application/octet-stream";
- /****
- * 関数定義
- */
- // mb_encode_mimeheaderのバグ対策用
- function mb_encode_mimeheader_ex($text, $split_count = 34) {
- $position = 0;
- $encorded = '';
- while ($position < mb_strlen($text, 'ISO-2022-JP')) {
- if ($encorded != '') {
- $encorded .= "\r\n ";
- }
- $output_temp = mb_strimwidth($text, $position, $split_count, '', 'ISO-2022-JP');
- $position = $position + mb_strlen($output_temp, 'ISO-2022-JP');
- $encorded .= "=?ISO-2022-JP?B?" . base64_encode($output_temp) . "?=";
- }
- return $encorded;
- }
- /****
- * 以下、実際の処理
- */
- // 文字エンコードの設定
- mb_internal_encoding('UTF-8');
- // マルチパートなので、パートの区切り文字列を指定
- $boundary = '----=_Boundary_' . uniqid(rand(1000,9999) . '_') . '_';
- // 件名のエンコード
- $subject = mb_convert_encoding($subject, 'ISO-2022-JP', 'UTF-8');
- $subject = mb_encode_mimeheader_ex($subject);
- // 本文のエンコード
- $message = mb_convert_encoding($message, 'ISO-2022-JP', 'UTF-8');
- // toをエンコード
- $to = mb_convert_encoding($mail['to']['name'], "JIS", "UTF-8");
- $to = "=?ISO-2022-JP?B?" . base64_encode($to) . '?= <' . $mail['to']['mail'] . '>';
- // fromをエンコード
- $from = mb_convert_encoding($mail['from']['name'], "JIS", "UTF-8");
- $from = "=?ISO-2022-JP?B?" . base64_encode($from) . '?= <' . $mail['from']['mail'] . '>';
- // 添付ファイルのエンコード
- $filename = mb_convert_encoding($filename, 'ISO-2022-JP', 'UTF-8');
- $filename = "=?ISO-2022-JP?B?" . base64_encode($filename) . "?=";
- // ファイルを開く
- if ($handle = @fopen($attach_file, "r")) {
- while (!feof($handle)) {
- $attach_file .= fread($handle, 14096);
- }
- fclose($handle);
- }
- $attach_file = chunk_split(base64_encode($attach_file), 76, "\n"); // Base64に変換し76Byte分割
- // ヘッダーの指定
- $head = '';
- $head .= "From: {$from}\n";
- $head .= "MIME-Version: 1.0\n";
- $head .= "Content-Type: multipart/mixed; boundary=\"{$boundary}\"\n";
- $head .= "Content-Transfer-Encoding: 7bit";
- $body = '';
- // 本文
- $body .= "--{$boundary}\n";
- $body .= "Content-Type: text/plain; charset=ISO-2022-JP\n" .
- "Content-Transfer-Encoding: 7bit\n";
- $body .= "\n";
- $body .= "{$message}\n";
- // 添付ファイルの処理
- $body .= "--{$boundary}\n";
- $body .= "Content-Type: {$mime_type}; name=\"{$filename}\"\n" .
- "Content-Transfer-Encoding: base64\n" .
- "Content-Disposition: attachment; filename=\"{$filename}\"\n";
- $body .= "\n";
- $body .= "{$attach_file}\n";
- // マルチパートの終了
- $body .= "--$boundary--\n";
- if (mail($to, $subject, $body, $head)) {
- echo '送信完了';
- } else {
- echo '送信できませんでした。';
- }
Linux raq.nsk.dom 2.4.20-42.9.legacy #1 Sun Feb 20 14:08:04 EST 2005 i686 i686 i386 GNU/Linux
/usr/sbin/sendmail -t -i
PHPのバージョンUPは不可
PEARも利用不可
情報が不足しておりましたら、ご指摘下さい。
宜しくお願いいたします。
この質問への意見の募集は締め切られ、ポイントは既に配分されました。
意見を投稿することはできますが、ポイントを受け取ることはできません。





ページのトップへ


今回のような実践的な経験がエンジニアのキャリアに繋がると思います。是非サービスを成功させて下さい!