将以下代码复制并粘贴到您的 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 处理程序 (mail_handler.php):
<?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. } ?>
如果您希望发送 HTML 电子邮件,请创建具有不同变量名称的单独 HTML 标头。有关详细信息,请参阅 PHP mail() 手册。
如果您在发送电子邮件时遇到问题,请参阅此 Stack Overflow 问题和解答:https://stackoverflow.com/questions/ 13061936/php-mail-form-doesnt-complete-sending-e-mail
以上是如何使用 PHP 从 HTML 表单发送电子邮件?的详细内容。更多信息请关注PHP中文网其他相关文章!