前提
メールフォームで添付したファイルだけがメールに添付されません。
他のPOSTデータ(名前など)はメール本文に問題なく表示されます。
実現したいこと
問合せフォームに添付した画像ファイルをメール送信したい。
発生している問題・エラーメッセージ
アップロードしたファイルがメールに添付されない。
該当のソースコード
php
<?php define( "FILE_DIR", "images/img/"); // ファイルのアップロード if( !empty($_FILES['photos']['tmp_name']) ) { $upload_res = move_uploaded_file( $_FILES['photos']['tmp_name'], FILE_DIR.$_FILES['photos']['name']); if( $upload_res !== true ) { $error[] = 'ファイルのアップロードに失敗しました。'; } else { $photos = $_FILES['photos']['name']; } } //取得した値を変数に代入 $name =$_POST['name']; $tel = $_POST['tel']; $email = $_POST['email']; $detail = $_POST['detail']; // 変数とタイムゾーンを初期化 $header = null; $body = null; $auto_reply_subject = null; $auto_reply_text = null; $admin_reply_subject = null; $admin_reply_text = null; date_default_timezone_set('Asia/Tokyo'); //日本語の使用宣言 mb_language("ja"); mb_internal_encoding("UTF-8"); $header = "MIME-Version: 1.0\n"; $header = "Content-Type: multipart/mixed;boundary=\"__BOUNDARY__\"\n"; $header .= "From: メール <mailaddress>\n"; $header .= "Reply-To: メール <mailaddress>\n"; // 件名を設定 $auto_reply_subject = 'お問い合わせありがとうございます。'; // 本文を設定 $auto_reply_text = "この度は、お問い合わせ頂き誠にありがとうございます。\n\n"; $auto_reply_text .= "お問い合わせ日時:" . date("Y-m-d H:i") . "\n"; $auto_reply_text .= "お名前:" . $name. "\n"; $auto_reply_text .= "電話番号:" . $tel. "\n"; $auto_reply_text .= "メールアドレス:" . $email. "\n"; $auto_reply_text .= "内容:" . $detail. "\n"; $auto_reply_text .= "OWNER"; // テキストメッセージをセット $body = "--__BOUNDARY__\n"; $body .= "Content-Type: text/plain; charset=\"ISO-2022-JP\"\n\n"; $body .= $auto_reply_text . "\n"; $body .= "--__BOUNDARY__\n"; // ファイルを添付 if( !empty($photos) ) { $body .= "Content-Type: application/octet-stream; name=\"{$photos}\"\n"; $body .= "Content-Disposition: attachment; filename=\"{$photos}\"\n"; $body .= "Content-Transfer-Encoding: base64\n"; $body .= "\n"; $body .= chunk_split(base64_encode(file_get_contents($photos))); $body .= "--__BOUNDARY__\n"; } // 自動返信メール送信 mb_send_mail( $email, $auto_reply_subject, $body, $header); // 運営側へ送るメールの件名 $admin_reply_subject = "お問い合わせを受け付けました"; // 本文を設定 $admin_reply_text = "下記の内容でお問い合わせがありました。\n\n"; $admin_reply_text .= "お問い合わせ日時:" . date("Y-m-d H:i") . "\n"; $admin_reply_text .= "お名前:" . $name. "\n"; $admin_reply_text .= "電話番号:" . $tel. "\n"; $admin_reply_text .= "メールアドレス:" . $email. "\n"; $admin_reply_text .= "内容:" . $detail. "\n"; // テキストメッセージをセット $body = "--__BOUNDARY__\n"; $body .= "Content-Type: text/plain; charset=\"ISO-2022-JP\"\n\n"; $body .= $admin_reply_text . "\n"; $body .= "--__BOUNDARY__\n"; // ファイルを添付 if( !empty($photos) ) { $body .= "Content-Type: application/octet-stream; name=\"{$photos}\"\n"; $body .= "Content-Disposition: attachment; filename=\"{$photos}\"\n"; $body .= "Content-Transfer-Encoding: base64\n"; $body .= "\n"; $body .= chunk_split(base64_encode(file_get_contents($photos))); $body .= "--__BOUNDARY__\n"; } // 管理者へメール送信 mb_send_mail( 'mailaddress', $admin_reply_subject, $body, $header); ?>
html5
<form action="contact.php" enctype="multipart/form-data" id="contact_form" method="POST"> <p class="form_label">お名前</p> <input type="text" name="name"> <p class="form_label">電話番号</p> <input type="text" name="tel"> <p class="form_label">メールアドレス</p> <input type="text" name="email"> <p class="form_label">内容</p> <textarea name="detail"></textarea> <div class="upload_box"> <p>写真添付</p> <label for="file" class="upload"> 画像選択 </label> <input id="file" type="file" name="photos"> </div> <button id="contact_button" type="submit">送信</button> </form>
届いたメール
下記の内容でお問い合わせがありました。
お問い合わせ日時:2022-08-06 10:06
お名前:テスト
電話番号:テスト
メールアドレス:テスト
内容:テスト
試したこと
サーバー側で書き込みなどの設定を変更しても変わらず。
php.iniにてサイズや、アップロードに関する内容を変更しましたがだめでした。
補足情報(FW/ツールのバージョンなど)
メールの内容はファイル以外全て問題なく届いています。
よろしくお願いします。
まだ回答がついていません
会員登録して回答してみよう