將以下程式碼複製並貼上到您的HTML 表單中:
<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>
<?php if (isset($_POST['submit'])) { $to = "[email protected]"; // Replace with your email address $from = $_POST['email']; // Fetching 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); // Send a copy 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 a thank you page. } ?>
以上是如何使用 PHP 從 HTML 表單發送電子郵件?的詳細內容。更多資訊請關注PHP中文網其他相關文章!