当用户填写完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";
}
}
?>
Run Code Online (Sandbox Code Playgroud)
用PHP发送电子邮件的代码是什么?
Fun*_*ner 134
如果我理解正确,您希望将所有内容放在一个页面中并从同一页面执行.
您可以使用下面的代码从单个页面发送邮件,例如index.php或contact.php
这个和我原来的答案之间的唯一区别是<form action="" method="post">动作留空的位置.
最好在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>
Run Code Online (Sandbox Code Playgroud)
我不太确定问题是什么,但我的印象是将邮件的副本发送给填写表格的人.
这是HTML表单和PHP处理程序的测试/工作副本.这使用PHP mail()函数.
PHP处理程序还会将消息的副本发送给填写表单的人员.
//如果您不打算使用它,可以在一行代码前面使用两个正斜杠.
例如: // $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>
Run Code Online (Sandbox Code Playgroud)
(使用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.
}
?>
Run Code Online (Sandbox Code Playgroud)
要以HTML格式发送:
如果您希望以HTML格式发送邮件以及两个实例,则需要创建两组具有不同变量名称的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文件扩展名等.
如果发送邮件失败,请参阅堆栈上的以下问答:
在 Windows 中使用 PHP 发送电子邮件有点像雷区,充满陷阱和令人头疼。我将尝试向您介绍一个实例,在该实例中,我让它在 (IIS) Internet 信息服务网络服务器下的 Windows 7 和 PHP 5.2.3 上运行。
我假设您不想使用任何包含电子邮件发送功能的预构建框架,例如 CodeIgniter 或 Symfony。我们将从独立的 PHP 文件发送电子邮件。我从 codeigniter 引擎盖下(在系统/库下)获取了此代码并对其进行了修改,以便您只需放入此 Email.php 文件即可,它应该可以正常工作。
这应该适用于较新版本的 PHP。但你永远不知道。
第 1 步,您需要 SMTP 服务器的用户名/密码:
smtp.ihostexchange.net我正在使用已经为我创建和设置的smtp 服务器。如果您没有这个,则无法继续。您应该能够使用Thunderbird、Evolution、Microsoft Outlook 等电子邮件客户端来指定您的 smtp 服务器,然后能够通过该服务器发送电子邮件。
第 2 步,创建您的 Hello World 电子邮件文件:
我假设您正在使用 IIS。因此,在下面创建一个名为index.php的文件C:\inetpub\wwwroot,并将以下代码放入其中:
<?php
include("Email.php");
$c = new CI_Email();
$c->from("FromUserName@foobar.com");
$c->to("user_to_receive_email@gmail.com");
$c->subject("Celestial Temple");
$c->message("Dominion reinforcements on the way.");
$c->send();
echo "done";
?>
Run Code Online (Sandbox Code Playgroud)
您应该能够通过在浏览器中导航到 localhost/index.php 来访问此 index.php,它会因为缺少 Email.php 而出现错误。但请确保您至少可以从浏览器运行它。
第 3 步,创建一个名为Email.php:
在 下创建一个名为 Email.php 的新文件C:\inetpub\wwwroot。
将此 PHP 代码复制/粘贴到 Email.php 中:
https://github.com/sentientmachine/standalone_php_script_send_email/blob/master/Email.php
由于 smtp 服务器有很多种,因此您必须手动修改 .smtp 顶部的设置Email.php。我已经将其设置为自动与 一起使用smtp.ihostexchange.net,但您的 smtp 服务器可能会有所不同。
例如:
\n是必需的。链接的代码太长,无法粘贴为 stackoverflow 答案,如果您想编辑它,请在此处或通过 github 发表评论,我会更改它。
第 4 步,确保您的 php.ini 已启用 ssl 扩展:
找到您的 PHP.ini 文件并取消注释
;extension=php_openssl.dll
Run Code Online (Sandbox Code Playgroud)
所以它看起来像:
extension=php_openssl.dll
Run Code Online (Sandbox Code Playgroud)
第5步,在浏览器中运行刚刚创建的index.php文件:
您应该得到以下输出:
220 smtp.ihostexchange.net Microsoft ESMTP MAIL Service ready at
Wed, 16 Apr 2014 15:43:58 -0400 250 2.6.0
<534edd7c92761@summitbroadband.com> Queued mail for delivery
lang:email_sent
done
Run Code Online (Sandbox Code Playgroud)
第 6 步,检查您的电子邮件和垃圾邮件文件夹:
访问 user_to_receive_email@gmail.com 的电子邮件帐户,您应该已经收到一封电子邮件。它应该在 5 或 10 秒内到达。如果没有,请检查页面上返回的错误。如果这不起作用,请尝试将脸敲在谷歌键盘上,同时高喊:“在杂货店工作也不错。”
| 归档时间: |
|
| 查看次数: |
562803 次 |
| 最近记录: |