PHP mail() 没有运行
PHP mail() is not functioning
自从过去两天以来,我一直在尝试在 JavaScript 和 PHP 之间进行通信。在阅读了其他人遇到的类似问题的大量教程、建议和解决方案后,我仍无法按预期成功。可以这么说,我是在我的域上编程,而不是在本地。
目标是:
从我的 JavaScript 中的 well-formed object 向我的 e-mail 地址发送一个格式良好的字符串。为了做到这一点,我明白需要使用著名的 email() 函数来执行 PHP 脚本。
实现目标的步数:
- 有一个功能正常的 JavaScript(没有错误)创建数据并将其填充到 above-mentioned JS object(命名为:'person');
- 将它格式化为一个好的字符串(因为我希望它以纯文本格式发送,而不是 HTML);
- 让jQuery的$.ajax()调用CORS 与 server-side 的交互,这是一个 PHP,包含必要的 Headers 和将简单邮件发送到指定目的地(例如我的 e-mail 地址)的代码。
- (可选)已进行 GZIP 压缩,缩小 HTML/CSS/JS 以更快地向客户端加载内容。
有什么问题?
我的 AJAX 代码运行平稳,没有错误,但未达到预期和预期。它没有 jumping/entering 进入 SUCCESS,而是立即出现错误 custom-made 函数,我似乎无法破译可能出了什么问题。
我将在 JS(AJAX 调用)和我用来实现上述目标的 PHP 脚本下面post。
JavaScript(已更新):
function submitOnClick() {
var full_name = "John Doe";
var profile_person = "--[Name: John, Surname: Doe, Birthday: 25/02/1950]--";
var hr = new XMLHttpRequest();
hr.open("POST", "submit_to_email.php", true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.send("full_name=" + full_name + "&" + "person=" + profile_person);
hr.onreadystatechange = function () {
if (hr.readyState == 4 && hr.status == 200) {
var data = hr.responseText;
console.log("PHP responded with: " + data);
}
}
}
PHP 脚本(已更新):
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
header('Content-Type: application/json');
$postvars = $_POST['person'];
//var_dump($postvars);
if (isset($postvars)) {
$to = 'to@gmail.com';
$subject = 'Subject Title';
$email_message = $postvars;
$headers = 'From: noreply@mydomain.com'."\r\n".
'Reply-To: noreply@mydomain.com'."\r\n" .
'X-Mailer: PHP/' . phpversion();
$checker = @mail($to, $subject, $email_message, $headers);
if ($checker == true) {
echo 'Mail was sent to'.$to;
}
else {
echo 'Mail was NOT sent to'.$to;
}
}
?>
<?php
die();
?>
更新
$.ajax() 根本没有必要,而是开始使用带有 XMLHttpRequest() 方法的普通 JS。内容已成功发送到我的 PHP 文件,但通过检查 mail() 是否为 true/false (sent/not) 导致 FALSE 。需要一个配置文件来提供 PHP 我的 SMTP 设置信息。我正在使用 Arvixe 托管,并且其中安装了 IIS 服务器。我对网络托管和维护域有点陌生,因此我不知道是否应该在我的项目文件夹中配置 web.config 文件或添加扩展 PHP 配置文件。
我认为您唯一需要将相关代码 200 的成功 header 作为答案。
如果您的回答是 json 格式 jquery 阅读该代码以重定向到成功或错误处理程序。
检查headerphp函数:
告诉我是否可行。
经过几天的研究,我终于找到了一种可行且非常有效的解决方案。问题在于我的域在具有 IIS 托管的 Windows Server 2008+ 上设置为 运行,因此 PHP 的 mail() 函数不完整,无法执行邮件。
解决方案过去和现在都是:PHPMailer 一个非常常见且使用率最高的 php 库,专门用于为开发人员提供自信和简单的代码。我从 GitHub source as a zip and then extract it. After that created a blank new php file and copy/paste the demo script on how-to send a mail and altered it to my needs. Finally uploaded both PHPMailer folder and the newly created script to my domain. After executing the script manually I've received mail(s) in my inbox and not as a spam. This tutorial 下载了它
也救了我一命。
脚本如下:
<? php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
header('Content-Type: application/json');
$postvars = $_POST['person'];
if (isset($postvars)) {
$to = 'recipient@gmail.com';
$subject = 'Some Title';
$email_message = $postvars;
require 'PHPMailerAutoload.php'; // The script needed to be called in order to executed
// further commands below
$mail = new PHPMailer;
$mail - > isSMTP(); // Set mailer to use SMTP
$mail - > Host = 'smtp.mydomain.com'; // Specify main and backup SMTP servers
$mail - > SMTPAuth = true; // Enable SMTP authentication
$mail - > Username = 'mymail@mydomain.com'; // SMTP username
$mail - > Password = 'password_here'; // SMTP password
$mail - > SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail - > Port = 465; // TCP port to connect to
$mail - > CharSet = 'UTF-8'; // Character set specified because I use "ë" and other ones
$mail - > From = 'mymail@mydomain.com'; // The same email address as specified above for authentication
$mail - > FromName = 'Myself'; // The name of the sender (optional)
$mail - > addAddress($to, 'recipients_name'); // Add a recipient & name (optional)
$mail - > addReplyTo('mymail@mydomain.com', 'Some text'); // To who to reply
$mail - > WordWrap = 70; // Wrapping words
$mail - > isHTML(true); // if TRUE , format is HTML or FALSE, format is Plain Text
$mail - > Subject = $subject;
$mail - > Body = $email_message; //Plain or HTML formatted string
$mail - > AltBody = $email_message; //The same as Body BUT it has to be Plain text always
if (!$mail - > send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: '.$mail - > ErrorInfo;
} else {
echo 'Message has been sent';
}
}
?>
自从过去两天以来,我一直在尝试在 JavaScript 和 PHP 之间进行通信。在阅读了其他人遇到的类似问题的大量教程、建议和解决方案后,我仍无法按预期成功。可以这么说,我是在我的域上编程,而不是在本地。
目标是:
从我的 JavaScript 中的 well-formed object 向我的 e-mail 地址发送一个格式良好的字符串。为了做到这一点,我明白需要使用著名的 email() 函数来执行 PHP 脚本。
实现目标的步数:
- 有一个功能正常的 JavaScript(没有错误)创建数据并将其填充到 above-mentioned JS object(命名为:'person');
- 将它格式化为一个好的字符串(因为我希望它以纯文本格式发送,而不是 HTML);
- 让jQuery的$.ajax()调用CORS 与 server-side 的交互,这是一个 PHP,包含必要的 Headers 和将简单邮件发送到指定目的地(例如我的 e-mail 地址)的代码。
- (可选)已进行 GZIP 压缩,缩小 HTML/CSS/JS 以更快地向客户端加载内容。
有什么问题?
我的 AJAX 代码运行平稳,没有错误,但未达到预期和预期。它没有 jumping/entering 进入 SUCCESS,而是立即出现错误 custom-made 函数,我似乎无法破译可能出了什么问题。
我将在 JS(AJAX 调用)和我用来实现上述目标的 PHP 脚本下面post。
JavaScript(已更新):
function submitOnClick() {
var full_name = "John Doe";
var profile_person = "--[Name: John, Surname: Doe, Birthday: 25/02/1950]--";
var hr = new XMLHttpRequest();
hr.open("POST", "submit_to_email.php", true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.send("full_name=" + full_name + "&" + "person=" + profile_person);
hr.onreadystatechange = function () {
if (hr.readyState == 4 && hr.status == 200) {
var data = hr.responseText;
console.log("PHP responded with: " + data);
}
}
}
PHP 脚本(已更新):
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
header('Content-Type: application/json');
$postvars = $_POST['person'];
//var_dump($postvars);
if (isset($postvars)) {
$to = 'to@gmail.com';
$subject = 'Subject Title';
$email_message = $postvars;
$headers = 'From: noreply@mydomain.com'."\r\n".
'Reply-To: noreply@mydomain.com'."\r\n" .
'X-Mailer: PHP/' . phpversion();
$checker = @mail($to, $subject, $email_message, $headers);
if ($checker == true) {
echo 'Mail was sent to'.$to;
}
else {
echo 'Mail was NOT sent to'.$to;
}
}
?>
<?php
die();
?>
更新
$.ajax() 根本没有必要,而是开始使用带有 XMLHttpRequest() 方法的普通 JS。内容已成功发送到我的 PHP 文件,但通过检查 mail() 是否为 true/false (sent/not) 导致 FALSE 。需要一个配置文件来提供 PHP 我的 SMTP 设置信息。我正在使用 Arvixe 托管,并且其中安装了 IIS 服务器。我对网络托管和维护域有点陌生,因此我不知道是否应该在我的项目文件夹中配置 web.config 文件或添加扩展 PHP 配置文件。
我认为您唯一需要将相关代码 200 的成功 header 作为答案。
如果您的回答是 json 格式 jquery 阅读该代码以重定向到成功或错误处理程序。
检查headerphp函数:
告诉我是否可行。
经过几天的研究,我终于找到了一种可行且非常有效的解决方案。问题在于我的域在具有 IIS 托管的 Windows Server 2008+ 上设置为 运行,因此 PHP 的 mail() 函数不完整,无法执行邮件。
解决方案过去和现在都是:PHPMailer 一个非常常见且使用率最高的 php 库,专门用于为开发人员提供自信和简单的代码。我从 GitHub source as a zip and then extract it. After that created a blank new php file and copy/paste the demo script on how-to send a mail and altered it to my needs. Finally uploaded both PHPMailer folder and the newly created script to my domain. After executing the script manually I've received mail(s) in my inbox and not as a spam. This tutorial 下载了它 也救了我一命。 脚本如下:
<? php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
header('Content-Type: application/json');
$postvars = $_POST['person'];
if (isset($postvars)) {
$to = 'recipient@gmail.com';
$subject = 'Some Title';
$email_message = $postvars;
require 'PHPMailerAutoload.php'; // The script needed to be called in order to executed
// further commands below
$mail = new PHPMailer;
$mail - > isSMTP(); // Set mailer to use SMTP
$mail - > Host = 'smtp.mydomain.com'; // Specify main and backup SMTP servers
$mail - > SMTPAuth = true; // Enable SMTP authentication
$mail - > Username = 'mymail@mydomain.com'; // SMTP username
$mail - > Password = 'password_here'; // SMTP password
$mail - > SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail - > Port = 465; // TCP port to connect to
$mail - > CharSet = 'UTF-8'; // Character set specified because I use "ë" and other ones
$mail - > From = 'mymail@mydomain.com'; // The same email address as specified above for authentication
$mail - > FromName = 'Myself'; // The name of the sender (optional)
$mail - > addAddress($to, 'recipients_name'); // Add a recipient & name (optional)
$mail - > addReplyTo('mymail@mydomain.com', 'Some text'); // To who to reply
$mail - > WordWrap = 70; // Wrapping words
$mail - > isHTML(true); // if TRUE , format is HTML or FALSE, format is Plain Text
$mail - > Subject = $subject;
$mail - > Body = $email_message; //Plain or HTML formatted string
$mail - > AltBody = $email_message; //The same as Body BUT it has to be Plain text always
if (!$mail - > send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: '.$mail - > ErrorInfo;
} else {
echo 'Message has been sent';
}
}
?>