当用户完成HTML表单的填写后,我想通过PHP发送电子邮件,然后通过电子邮件从表单中发送信息。我想从显示具有表单的网页的同一脚本中执行此操作。
我找到了此代码,但是邮件未发送。
<?php if (isset($_POST['submit'])) { $to = $_POST['email']; $subject = $_POST['name']; $message = getRequestURI(); $from = "zenphoto@example.com"; $headers = "From:" . $from; if (mail($to, $subject, $message, $headers)) { echo "Mail Sent."; } else { echo "failed"; } } ?>
用PHP发送电子邮件的代码是什么?
如果我理解正确,那么您希望将所有内容都放在一个页面中并从同一页面执行。
您可以使用下面的代码从单个页面发送邮件,例如index.php或contact.php
index.php
contact.php
此答案与我的原始答案之间的唯一区别是<form action="" method="post">操作保留为空白。
<form action="" method="post">
最好header('Location: thank_you.php');不要echo在PHP处理程序中使用,之后再将用户重定向到另一个页面。
header('Location: thank_you.php');
echo
<?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处理程序的经过测试/有效的副本。这使用了PHP mail()函数。
mail()
PHP处理程序还将把消息的副本发送给填写表格的人。
//如果您不打算使用它,则可以在一行代码前使用两个正斜杠。
//
例如: // $subject2 = "Copy of your form submission";将不执行。
// $subject2 = "Copy of your form submission";
<!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>
(使用来自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发送,并且对于这两个实例,则需要使用不同的变量名称创建两组独立的HTML标头。
阅读手册mail(),了解如何以HTML格式发送电子邮件:
脚注:
您必须使用action属性指定将处理提交的数据的服务的URL。
如在_4.10.1.3_ 下 所述,配置表单以与服务器进行通信 。
因此,action=""将无法在HTML5中使用。
action=""
正确的语法为:
action="handler.xxx"
action="http://www.example.com/handler.xxx"
请注意,这xxx将是用于处理该过程的文件类型的扩展名。这可能是一个.php,.cgi,.pl,.jsp文件扩展名等。
xxx
.php
.cgi
.pl
.jsp