使用相同的脚本提交时,从 html 表单使用 PHP 发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18379238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send email with PHP from html form on submit with the same script
提问by fusi0n
I want to send an email with PHP when a user has finished filling in an HTML form and then emailing information from the form. I want to do it from the same script that displays the web page that has the form.
我想在用户填写完 HTML 表单然后通过电子邮件从表单发送信息时使用 PHP 发送电子邮件。我想从显示具有表单的网页的同一个脚本中执行此操作。
I found this code, but the mail does not send.
我找到了这个代码,但是邮件没有发送。
<?php
if (isset($_POST['submit'])) {
$to = $_POST['email'];
$subject = $_POST['name'];
$message = getRequestURI();
$from = "[email protected]";
$headers = "From:" . $from;
if (mail($to, $subject, $message, $headers)) {
echo "Mail Sent.";
}
else {
echo "failed";
}
}
?>
What is the code to send an email in PHP?
在 PHP 中发送电子邮件的代码是什么?
回答by Funk Forty Niner
EDIT (#1)
编辑(#1)
If I understand correctly, you wish to have everything in one page and execute it from the same page.
如果我理解正确,您希望将所有内容都放在一页中并从同一页执行。
You can use the following code to send mail from a single page, for example index.php
or contact.php
您可以使用以下代码从单个页面发送邮件,例如index.php
或contact.php
The only difference between this one and my original answer is the <form action="" method="post">
where the action has been left blank.
这个和我原来的答案之间的唯一区别是<form action="" method="post">
动作被留空的地方。
It is better to use header('Location: thank_you.php');
instead of echo
in the PHP handler to redirect the user to another page afterwards.
最好在 PHP 处理程序中使用header('Location: thank_you.php');
而不是echo
在之后将用户重定向到另一个页面。
Copy the entire code below into one file.
将下面的整个代码复制到一个文件中。
<?php
if(isset($_POST['submit'])){
$to = "[email protected]"; // 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>
Original answer
原答案
I wasn't quite sure as to what the question was, but am under the impression that a copy of the message is to be sent to the person who filled in the form.
我不太确定问题是什么,但我的印象是该消息的副本将发送给填写表格的人。
Here is a tested/working copy of an HTML form and PHP handler. This uses the PHP mail()
function.
这是 HTML 表单和 PHP 处理程序的测试/工作副本。这使用了 PHPmail()
函数。
The PHP handler will also send a copy of the message to the person who filled in the form.
PHP 处理程序还会将消息的副本发送给填写表单的人员。
You can use two forward slashes //
in front of a line of code if you're not going to use it.
//
如果您不打算使用它,您可以在一行代码前使用两个正斜杠。
For example:// $subject2 = "Copy of your form submission";
will not execute.
例如:// $subject2 = "Copy of your form submission";
不会执行。
HTML FORM:
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 handler (mail_handler.php)
PHP 处理程序 (mail_handler.php)
(Uses info from HTML form and sends the Email)
(使用来自 HTML 表单的信息并发送电子邮件)
<?php
if(isset($_POST['submit'])){
$to = "[email protected]"; // 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.
}
?>
To send as HTML:
以 HTML 格式发送:
If you wish to send mail as HTML and for both instances, then you will need to create two separate sets of HTML headers with different variable names.
如果您希望以 HTML 格式发送邮件并且对于这两种情况,则需要创建两组具有不同变量名称的独立 HTML 标头集。
Read the manual on mail()
to learn how to send emails as HTML:
阅读手册mail()
以了解如何以 HTML 格式发送电子邮件:
Footnotes:
脚注:
- In regards to HTML5
- 关于 HTML5
You have to specify the URL of the service that will handle the submitted data, using the action attribute.
您必须使用 action 属性指定将处理提交数据的服务的 URL。
As outlined at https://www.w3.org/TR/html5/forms.htmlunder 4.10.1.3 Configuring a form to communicate with a server. For complete information, consult the page.
如https://www.w3.org/TR/html5/forms.html下4.10.1.3 配置表单以与服务器通信 中所述。有关完整信息,请参阅页面。
Therefore, action=""
will not work in HTML5.
因此,action=""
将无法在 HTML5 中工作。
The proper syntax would be:
正确的语法是:
action="handler.xxx"
oraction="http://www.example.com/handler.xxx"
.
action="handler.xxx"
或者action="http://www.example.com/handler.xxx"
.
Note that xxx
will be the extension of the type of file used to handle the process. This could be a .php
, .cgi
, .pl
, .jsp
file extension etc.
请注意,这xxx
将是用于处理该过程的文件类型的扩展名。这可能是.php
, .cgi
, .pl
,.jsp
文件扩展名等。
Consult the following Q&A on Stack if sending mail fails:
如果发送邮件失败,请参阅 Stack 上的以下问答:
回答by Eric Leschinski
PHP script to connect to a SMTP server and send email on Windows 7
用于连接到 SMTP 服务器并在 Windows 7 上发送电子邮件的 PHP 脚本
Sending an email from PHP in Windows is a bit of a minefield with gotchas and head scratching. I'll try to walk you through one instance where I got it to work on Windows 7 and PHP 5.2.3 under (IIS) Internet Information Services webserver.
在 Windows 中从 PHP 发送电子邮件有点像雷区,有陷阱和头疼。我将尝试向您介绍一个实例,在该实例中,我让它在 (IIS) Internet 信息服务网络服务器下的 Windows 7 和 PHP 5.2.3 上运行。
I'm assuming you don't want to use any pre-built framework like CodeIgniter or Symfony which contains email sending capability. We'll be sending an email from a standalone PHP file. I acquired this code from under the codeigniter hood (under system/libraries) and modified it so you can just drop in this Email.php file and it should just work.
我假设您不想使用任何包含电子邮件发送功能的预构建框架,如 CodeIgniter 或 Symfony。我们将从独立的 PHP 文件发送电子邮件。我从 codeigniter 引擎盖下(在系统/库下)获取了这段代码并修改了它,这样你就可以放入这个 Email.php 文件,它应该可以正常工作。
This should work with newer versions of PHP. But you never know.
这应该适用于较新版本的 PHP。但你永远不知道。
Step 1, You need a username/password with an SMTP server:
第 1 步,您需要使用 SMTP 服务器的用户名/密码:
I'm using the smtp server from smtp.ihostexchange.net
which is already created and setup for me. If you don't have this you can't proceed. You should be able to use an email client like thunderbird, evolution, Microsoft Outlook, to specify your smtp server and then be able to send emails through there.
我正在使用smtp.ihostexchange.net
已经为我创建和设置的 smtp 服务器。如果你没有这个,你就不能继续。您应该能够使用诸如 Thunderbird、evolution、Microsoft Outlook 之类的电子邮件客户端来指定您的 smtp 服务器,然后能够通过那里发送电子邮件。
Step 2, Create your Hello World Email file:
第 2 步,创建您的 Hello World 电子邮件文件:
I'm assuming you are using IIS. So create a file called index.php under C:\inetpub\wwwroot
and put this code in there:
我假设您正在使用 IIS。所以在下面创建一个名为 index.php 的文件C:\inetpub\wwwroot
并将这段代码放在那里:
<?php
include("Email.php");
$c = new CI_Email();
$c->from("[email protected]");
$c->to("[email protected]");
$c->subject("Celestial Temple");
$c->message("Dominion reinforcements on the way.");
$c->send();
echo "done";
?>
You should be able to visit this index.php by navigating to localhost/index.php in a browser, it will spew errors because Email.php is missing. But make sure you can at least run it from the browser.
你应该能够通过在浏览器中导航到 localhost/index.php 来访问这个 index.php,它会因为缺少 Email.php 而出现错误。但请确保您至少可以从浏览器运行它。
Step 3, Create a file called Email.php
:
第 3 步,创建一个名为 的文件Email.php
:
Create a new file called Email.php under C:\inetpub\wwwroot
.
在 .php 下创建一个名为 Email.php 的新文件C:\inetpub\wwwroot
。
Copy/paste this PHP code into Email.php:
将此 PHP 代码复制/粘贴到 Email.php 中:
https://github.com/sentientmachine/standalone_php_script_send_email/blob/master/Email.php
https://github.com/sentientmachine/standalone_php_script_send_email/blob/master/Email.php
Since there are many kinds of smtp servers, you will have to manually fiddle with the settings at the top of Email.php
. I've set it up so it automatically works with smtp.ihostexchange.net
, but your smtp server might be different.
由于有多种 smtp 服务器,您将不得不手动修改 .mtp 服务器顶部的设置Email.php
。我已经对其进行了设置,因此它可以自动与 一起使用smtp.ihostexchange.net
,但是您的 smtp 服务器可能会有所不同。
For example:
例如:
- Set the smtp_port setting to the port of your smtp server.
- Set the smtp_crypto setting to what your smtp server needs.
- Set the $newline and $crlf so it's compatible with what your smtp server uses. If you pick wrong, the smtp server may ignore your request without error. I use \r\n, for you maybe
\n
is required.
- 将 smtp_port 设置设置为 smtp 服务器的端口。
- 将 smtp_crypto 设置设置为您的 smtp 服务器需要的内容。
- 设置 $newline 和 $crlf 使其与您的 smtp 服务器使用的兼容。如果您选择错误,smtp 服务器可能会无误地忽略您的请求。我使用\r\n,因为你可能
\n
需要。
The linked code is too long to paste as a stackoverflow answer, If you want to edit it, leave a comment in here or through github and I'll change it.
链接的代码太长,无法粘贴为 stackoverflow 答案,如果您想编辑它,请在此处或通过 github 发表评论,我会更改它。
Step 4, make sure your php.ini has ssl extension enabled:
第 4 步,确保您的 php.ini 启用了 ssl 扩展:
Find your PHP.ini file and uncomment the
找到您的 PHP.ini 文件并取消注释
;extension=php_openssl.dll
So it looks like:
所以它看起来像:
extension=php_openssl.dll
Step 5, Run the index.php file you just made in a browser:
第五步,在浏览器中运行刚才创建的 index.php 文件:
You should get the following output:
你应该得到以下输出:
220 smtp.ihostexchange.net Microsoft ESMTP MAIL Service ready at
Wed, 16 Apr 2014 15:43:58 -0400 250 2.6.0
<[email protected]> Queued mail for delivery
lang:email_sent
done
Step 6, check your email, and spam folder:
第 6 步,检查您的电子邮件和垃圾邮件文件夹:
Visit the email account for [email protected] and you should have received an email. It should arrive within 5 or 10 seconds. If you does not, inspect the errors returned on the page. If that doesn't work, try mashing your face on the keyboard on google while chanting: "working at the grocery store isn't so bad."
访问 [email protected] 的电子邮件帐户,您应该已收到一封电子邮件。它应该在 5 或 10 秒内到达。如果没有,请检查页面上返回的错误。如果这不起作用,请尝试在谷歌键盘上捣碎你的脸,同时高呼:“在杂货店工作还不错。”
回答by KB9
If you haven't already, look at your php.ini
and make sure the parameters under the [mail function]
setting are set correctly to activate the email service. After you can use PHPMailerlibrary and follow the instructions.
如果您还没有,请查看您的php.ini
并确保[mail function]
设置下的参数设置正确以激活电子邮件服务。之后您可以使用PHPMailer库并按照说明进行操作。
回答by Hareesh
You can also use mandrill app to send the mail in php. You will get the API from https://mandrillapp.com/api/docs/index.php.htmlwhere you can find the complete details about emails sended and other details.
您也可以使用 mandrill 应用程序在 php 中发送邮件。您将从https://mandrillapp.com/api/docs/index.php.html获取 API ,您可以在其中找到有关已发送电子邮件的完整详细信息和其他详细信息。
回答by Hareesh
Here are the PHP mail settings I use:
以下是我使用的 PHP 邮件设置:
//Mail sending function
$subject = $_POST['name'];
$to = $_POST['email'];
$from = "[email protected]";
//data
$msg = "Your MSG <br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
mail($to,$subject,$msg,$headers);
echo "Mail Sent.";
回答by Moduo
You need to add an action
into your form like:
您需要action
在表单中添加一个,例如:
<form name='form1' method='post' action='<?php echo($_SERVER['PHP_SELF']);'>
<!-- All your input for the form here -->
</form>
Then put your snippet in the top of the document en send the mail. What echo($_SERVER['PHP_SELF']);
does is that it sends your information the top of your script so you could use it.
然后将您的片段放在文档的顶部,然后发送邮件。是什么echo($_SERVER['PHP_SELF']);
做的是,它会将您的信息,你的脚本的顶部,这样你可以使用它。
回答by Andrei CACIO
You need a SMPT Server in order for
您需要一个 SMPT 服务器才能
... mail($to,$subject,$message,$headers);
to work.
上班。
You could try light weight SMTP servers like xmailer
您可以尝试像 xmailer 这样的轻量级 SMTP 服务器
回答by Markus Amalthea Magnuson
I think one error in the original code might have been that it had:
我认为原始代码中的一个错误可能是它有:
$message = echo getRequestURI();
instead of:
代替:
$message = getRequestURI();
(The code has since been edited though.)
(尽管此代码已被编辑。)