編集 (#1)
私の理解が正しければ、すべてを 1 つのページにまとめて、同じページから実行したいと考えています。
次のコードを使用して、単一のページからメールを送信できます。たとえばindex.php
、またはcontact.php
これと私の元の答えの唯一の違いは<form action="" method="post">
、アクションが空白のままになっていることです。
後でユーザーを別のページにリダイレクトするには、PHP ハンドラーのheader('Location: thank_you.php');
代わりに使用することをお勧めします。echo
以下のコード全体を 1 つのファイルにコピーします。
<?php
if(isset($_POST['submit'])){
$to = "email@example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
元の答え
質問の内容がよくわかりませんでしたが、フォームに記入した人にメッセージのコピーが送信されるという印象を受けました。
これは、HTML フォームと PHP ハンドラーのテスト済み/作業用コピーです。これは PHPmail()
関数を使用します。
PHP ハンドラーは、フォームに入力した人にもメッセージのコピーを送信します。
//
スラッシュを使用しない場合は、コード行の前に2 つのスラッシュを使用できます。
例: // $subject2 = "Copy of your form submission";
実行されません。
HTMLフォーム:
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="mail_handler.php" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
PHP ハンドラ (mail_handler.php)
(HTML フォームからの情報を使用してメールを送信します)
<?php
if(isset($_POST['submit'])){
$to = "email@example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
// You cannot use header and echo together. It's one or the other.
}
?>
HTML として送信するには:
メールを HTML として両方のインスタンスで送信する場合は、異なる変数名を持つ 2 つの別個の HTML ヘッダー セットを作成する必要があります。
mail()
メールを HTML として送信する方法については、次のマニュアルを参照してください。
脚注:
action 属性を使用して、送信されたデータを処理するサービスの URL を指定する必要があります。
https://www.w3.org/TR/html5/forms.htmlの4.10.1.3 サーバーと通信するためのフォームの構成で概説されているとおりです。完全な情報については、ページを参照してください。
したがって、action=""
HTML5 では機能しません。
適切な構文は次のとおりです。
action="handler.xxx"
また
action="http://www.example.com/handler.xxx"
.
xxx
プロセスを処理するために使用されるファイルの種類の拡張子になることに注意してください。これは、、、、ファイル.php
拡張子などです。.cgi
.pl
.jsp
メールの送信に失敗した場合は、Stack に関する次の Q&A を参照してください。