使用相同的脚本通过html表单从PHP表单发送电子邮件

使用相同的脚本通过html表单从PHP表单发送电子邮件,第1张

使用相同的脚本通过html表单从PHP表单发送电子邮件

编辑(#1)

如果我理解正确,那么您希望将所有内容都放在一个页面中并从同一页面执行。

您可以使用下面的代码从单个页面发送邮件,例如

index.php
contact.php

这个答案和我的原始答案之间的唯一区别是

<form action="" method="post">
*** 作留为空白。

最好

header('Location: thank_you.php');
不要
echo
在PHP处理程序中使用,之后再将用户重定向到另一个页面。

将下面的整个代码复制到一个文件中。

<?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:" . "nn" . $_POST['message'];    $message2 = "Here is a copy of your message " . $first_name . "nn" . $_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()函数。

PHP处理程序还将把消息的副本发送给填写表格的人。

//如果您不打算使用它,则可以在一行代码前使用两个正斜杠。

例如: // $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:" . "nn" . $_POST['message'];    $message2 = "Here is a copy of your message " . $first_name . "nn" . $_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格式发送电子邮件:

脚注:

关于HTML5
您必须使用action属性指定将处理提交的数据的服务的URL。

如在4.10.1.3下所述,配置表单以与服务器进行通信。有关完整的信息,请参阅页面。

因此,action=”“将无法在HTML5中使用。

正确的语法为:

  • action=”handler.xxx” 要么
  • action="http://www.example.com/handler.xxx”。

请注意,这xxx将是用于处理该过程的文件类型的扩展名。这可能是一个.php,.cgi,.pl,.jsp文件扩展名等。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5641218.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存