使用 javascript 和 PHP 发送电子邮件的简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15605687/
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
Simple way to send email with javascript and PHP
提问by Curious Learner
I need to be able to fetch an email address from the user and then use php to send them an email with a link. I know there are some implementations using jquery or AJAX but i have no experience in either of those topics. I was hoping for something simple. thanks
我需要能够从用户那里获取电子邮件地址,然后使用 php 向他们发送带有链接的电子邮件。我知道有一些使用 jquery 或 AJAX 的实现,但我对这些主题都没有经验。我希望有一些简单的东西。谢谢
回答by Jonast92
"I need to be able to fetch an email address from the user"
“我需要能够从用户那里获取电子邮件地址”
Create a form:
创建表格:
<form action="emailHandler.php" method="POST" id="emailForm">
<label for="emailInput">Email: </label>
<input type="text" name="emailInput" id="emailInput" value="" />
<input type="submit" id="submitEmail" value="Submit Email" />
</form>
"and then use php to send them an email with a link"
“然后使用 php 向他们发送带有链接的电子邮件”
The submit button within the form will POST the value of the input field to the PHP script emailHandler.php
表单中的提交按钮会将输入字段的值 POST 到 PHP 脚本 emailHandler.php
"I know there are some implementations using jquery or AJAX but i have no experience in either of those topics"
“我知道有一些使用 jquery 或 AJAX 的实现,但我对这些主题都没有经验”
You don't need jQuery or AJAX for this, jQuery and AJAX are javascript topics (in which case, AJAX is about having you fetched the value from the HTML, then POST them to a PHP backend, and receive a JSON object which will tell the javascript whether it was successful or not, this is obiously NOT REQUIRED here but it CAN be used), you can simply use the built in PHP mail function: http://php.net/manual/en/function.mail.php
为此,您不需要 jQuery 或 AJAX,jQuery 和 AJAX 是 javascript 主题(在这种情况下,AJAX 是关于让您从 HTML 中获取值,然后将它们发布到 PHP 后端,并接收一个 JSON 对象,它将告诉javascript 是否成功,这显然不是必需的,但可以使用),您可以简单地使用内置的 PHP 邮件功能:http: //php.net/manual/en/function.mail.php
in emailHandler.php.
在 emailHandler.php 中。
I like to do it like this:
我喜欢这样做:
function spamcheck($field)
{
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return true;
}
else
{
return false;
}
}
function sendMail($toEmail, $fromEmail, $subject, $message)
{
$validFromEmail = spamcheck($fromEmail);
if($validFromEmail)
{
mail($toEmail, $subject, $message, "From: $fromEmail");
}
}
And do something like this in emailHandler.php:
在 emailHandler.php 中做这样的事情:
$email = isset($_POST['emailInput']) ? $_POST['emailInput'] : false;
if($email != false)
{
$yourEmail = "[email protected]";.
$subject = "Link";
$message = "The link and some message";
$success = sendMail($email, $yourEmail, $subject, $message);
}
In some cases you have to modify the PHP ini file, you can do it like this:
在某些情况下,您必须修改 PHP ini 文件,您可以这样做:
ini_set('SMTP' , 'smtp.example.com');
ini_set('smtp_port' , '25');
ini_set('username' , '[email protected]');
ini_set('password' , 'password');
ini_set('sendmail_from' , '[email protected]');
". . I was hoping for something simple. thanks"
“. . 我希望有一些简单的东西。谢谢”
If this wasn't simple, then I don't know what is. If you wan't to make it complex, using jQuery and Ajax, then read about them online (or take a look at my profile, I've given out a lot of full working code that works with it).
如果这不简单,那么我不知道是什么。如果您不想让它变得复杂,请使用 jQuery 和 Ajax,然后在线阅读有关它们的信息(或查看我的个人资料,我已经给出了许多与它配合使用的完整工作代码)。
回答by nurf
If you are trying to send an email in a form, you can just do this to the form action and the rest are similar. Hope it helps(:
如果您尝试在表单中发送电子邮件,您可以只对表单操作执行此操作,其余操作类似。希望能帮助到你(:
<form action="MAILTO:[email protected]" method="post" enctype="text/plain">
回答by rgamber
PHP has a mail function. You will need to configure the php.inifor this though.
PHP 有邮件功能。不过,您需要为此配置 php.ini。