下記に3つのファイルのソースを置いています。mail.phpに自分のメールアドレスをいれて、3つをサーバー上で使うと自分のアドレス宛にメールを受け取ることができると思います。
しかしenterキーで改行せずに文章を打てば普通に届くのですが、改行したり「(´∀`)(;゚Д゚)!」←こんな顔文字を使ったりするとメッセージを受け取れません。
この状況で特にエラーが出るわけでもないですし、送信完了画面までたどり着いているのですがメールが来ていませんでした。
そもそもこのメールフォームは確認画面付きで、送信内容は名前と内容のみ。しかも名前は入力しなくても送信可能ということを希望として作成しています。
ですが相手側が改行したり絵文字を使わないと言い切れないので、そうなった場合でも内容を受信できるようにしたいです。
ソースのどこに何を追加したら改善されるでしょうか?
ファイル名 index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link rel="stylesheet" type="text/css" href="style.css"> </head> <title> mail </title> <body> <div class="tb-cell mail-form"> <form id="form" action="mail.php" method="post"> <div class="row"> <div class="cell"> <label>name (無記入可)</label> <!--cell--></div> <div class="cell"> <input type="text" name="name"> <!--cell--></div> <!--row--></div> <div class="row"> <div class="cell"> <label>comment</label> <!--cell--></div> <div class="cell"> <textarea name="comment" required></textarea> <!--cell--></div> <!--row--></div> <div id="BOX1"> <button type="submit" id="sbtn" name="action" value="post">確認</button> <button type="reset" id="rbtn">リセット</button> </div> </form> </body> </html>
ファイル名 mail.php
<!DOCTYPE html> <?php if(!$_POST){ header('Location: /'); } session_start(); if(isset($_POST['comment'])){ $_SESSION['name'] = $_POST['name']; $_SESSION['comment'] = $_POST['comment']; } ?> <html> <head> <meta charset="UTF-8"> <link rel="stylesheet" type="text/css" href="style.css"> </head> <title> mail </title> <body> <?php $action = $_POST['action']; $name = htmlspecialchars($_SESSION['name']); $comment = htmlspecialchars($_SESSION['comment']); $to = 'ここに受信者のメールアドレス'; $subject = '新着メッセージ'; $message = '[お名前]'."\n".$name."\n"; $message .= '[コメント]'."\n".$comment."\n\n\n"; $header = 'From: '.$comment."\r\n"; $header .= 'Reply-To: '.$comment."\r\n"; if($action == "post"){ echo '<div class="tb-cell mail-form">'; echo '<form id="form" action="mail.php" method="post">'; echo '<div class="row">'; echo '<div class="cell">'; echo '<label>name (無記入可)</label>'; echo '<!--cell--></div>'; echo '<div class="cell cell2">'; echo $_SESSION['name']; echo '<!--cell--></div>'; echo '<!--row--></div>'; echo '<div class="row">'; echo '<div class="cell">'; echo '<label>comment</label>'; echo '<!--cell--></div>'; echo '<div class="cell cell2">'; echo $_SESSION['comment']; echo '<!--cell--></div>'; echo '<!--row--></div>'; echo ' '; echo '<p class="kakunin">この内容で送信しますか?</p><br>'; echo '<div id="BOX1"><button type="submit" id="sbtn" name="action" value="send">send</button>'; echo '<button type="button" onclick="history.go(-1)">back</button></div>'; echo '<!--cell--></div>'; echo '<!--row--></div>'; echo '</form>'; echo '<!--tb-cell--></div>'; }elseif($action == "send"){ $status = mb_send_mail($to, $subject, $message, $header); if ($status) { echo '<div id="BOX1"><p class="msg">送信完了</p>'; echo '<button type="button" onclick="history.go(-2)">入力フォームに戻る</button></div>'; } else { echo '<p class="msg">メッセージの送信に失敗しました</p>'; echo '<button type="button" onclick="history.go(-2)">入力フォームに戻る</button>'; } $_SESSION = array(); session_destroy(); } ?> </body> </html>
ファイル名 style.css
.tb-cell { display: table; width: 300px; margin: 10px auto 10px auto; text-align: left; } .tb-cell .row { display: table-row; } .tb-cell .row .cell { display: table-cell; border: 1px solid #7e7e7e; padding: 10px; vertical-align: middle; color: #7e7e7e; } .tb-cell .row .cell:nth-child(odd) { width: 100px; } .tb-cell .row .cell:nth-child(even) { width: 200px; } .mail-form .row .cell { padding: 5px; } .mail-form .row .cell:nth-child(1) { background: rgb(187, 64, 95); } .mail-form .row .cell:nth-child(2) { background: rgb(245, 245, 245); } input[type="text"], input[type="email"] { height: 20px; font-size: 0.8rem; color: #7e7e7e; } textarea { height: 100px; font-size: 0.8rem; color: #7e7e7e; resize: none; } button { color: rgb(187, 64, 95); border: none; padding: 10px; font-size: 0.9rem; cursor: pointer; } button[type="button"] { margin-left: 10px; } button[type="button"], button[type="submit"] { background: #ffffff; } button[type="reset"] { background: none; } button[type="button"]:hover, button[type="submit"]:hover { background: rgb(243, 241, 242); } button[type="reset"]:hover { text-decoration: underline; } @media (min-width: 768px) { .tb-cell { width: 530px; } .tb-cell .row { display: table-row; } .tb-cell .row .cell { display: table-cell; border: 1px solid rgb(255, 255, 255); padding: 10px; vertical-align: middle; color: #ffffff; } .tb-cell .row .cell:nth-child(odd) { width: 130px; } .tb-cell .row .cell:nth-child(even) { width: 350px; } input[type="text"], input[type="email"] { width: 270px; } textarea { width: 350px; } #BOX1 { text-align: center; } label { font-size: 0.8rem; } .kakunin{ text-align: center; color: #7e7e7e; font-size: 0.9rem; margin-bottom: -10px; } .msg { color: #7e7e7e; font-size: 0.9rem; } .cell2 { color: #7e7e7e !important; font-size: 0.9rem !important; } /* ** */}
回答4件
あなたの回答
tips
プレビュー