phpでサクッとメールフォームを作る
メールフォームの実装だけが必要だって時にサクッと出来るメールフォームです。
とりあえず作る
Html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<form class="mailForm" action="mail.php" method="post"> <div class="mailForm__group"> <label for="name">お名前・会社名</label> <input type="text" id="name" name="name" required/> </div> <div class="mailForm__group"> <label for="name">メールアドレス</label> <input type="text" id="email" name="email" required/> </div> <div class="mailForm__group"> <label for="name">内容</label> <textarea type="text" id="content" name="content" required></textarea> </div> <input type="hidden" id="token" name="token" value="1234567" /> <button class="mailForm__submit" type="submit">送信</button> </form> |
cssは自由に装飾してください。
一応
1 |
<input type="hidden" id="token" value="1234567" /> |
でトークンを付けてます。
php
mail.phpという名前で下記を保存ください。
他に項目が必要であればhtmlと合わして増やしてください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<?php // パラメータ取得 $request_param = $_POST; // お問い合わせ日時 $request_datetime = date("Y年m月d日 H時i分s秒"); //自動返信メール $mailto = $request_param['email']; $to = 'メールを受け付けるメールアドレスを入力'; //ここを入力 $mailfrom = "From:自動送信メールを送信するメールアドレスを入力"; //ここを入力 $subject = "お問い合わせ有難うございます。"; $content = ""; $content .= $request_param['name']. "様\r\n"; $content .= "お問い合わせ有難うございます。\r\n"; $content .= "お問い合わせ内容は下記通りでございます。\r\n"; $content .= "=================================\r\n"; $content .= "お名前 " . htmlspecialchars($request_param['name'])."\r\n"; $content .= "メールアドレス " . htmlspecialchars($request_param['email'])."\r\n"; $content .= "内容 " . htmlspecialchars($request_param['content'])."\r\n"; $content .= "お問い合わせ日時 " . $request_datetime."\r\n"; $content .= "=================================\r\n"; //管理者確認用メール $subject2 = "お問い合わせがありました。"; $content2 = ""; $content2 .= "お問い合わせがありました。\r\n"; $content2 .= "お問い合わせ内容は下記通りです。\r\n"; $content2 .= "=================================\r\n"; $content2 .= "お名前 " . htmlspecialchars($request_param['name'])."\r\n"; $content2 .= "メールアドレス " . htmlspecialchars($request_param['email'])."\r\n"; $content2 .= "内容 " . htmlspecialchars($request_param['content'])."\r\n"; $content2 .= "お問い合わせ日時 " . $request_datetime."\r\n"; $content2 .= "================================="."\r\n"; mb_language("ja"); mb_internal_encoding("UTF-8"); //mail 送信 if($request_param['token'] === '1234567'){ if(mb_send_mail($to, $subject2, $content2, $mailfrom)){ mb_send_mail($mailto,$subject,$content,$mailfrom); ?> <script> window.location = 'メールを送信した後に表示されるページのurl'; </script> <?php } else { header('Content-Type: text/html; charset=UTF-8'); echo "メールの送信に失敗しました"; }; } else { echo "メールの送信に失敗しました(トークンエラー)"; } ?> |
メールフォームから送信された内容は、
1 |
$request_param = $_POST; |
で【$request_param】に格納されているので、増やすのであれば、
1 |
$content .= "お名前 " . htmlspecialchars($request_param['name'])."\r\n"; |
こんな感じで、htmlの【name=””】の所の値を、上の[‘name’]の所に入れてやってください。
htmlのtokenを変更したら、
1 2 |
//mail 送信 if($request_param['token'] === '1234567'){ |
の数字も変えてあげてください。
後は、メールアドレスの【 //ここを入力】の所を2つ入力すれば完成です。
後は設置するだけです
よろしければコメントをどうぞ