Javascript PHP mail()不起作用

Javascript PHP mail()不起作用,javascript,php,email,smtp,Javascript,Php,Email,Smtp,自从上两天以来,我一直在尝试JavaScript和PHP之间的通信。在阅读了大量关于其他人遇到的类似问题的教程、建议和解决方案后,我还无法如愿成功。可以说,我是在我的一个域上编程,而不是在本地 目标是: 从JavaScript中格式良好的对象向我的电子邮件地址发送一个格式非常好的字符串。为了做到这一点,我知道需要使用PHP脚本著名的电子邮件功能来执行它 为实现这一目标采取的步骤: 有一个功能正常的JavaScript,没有错误,可以创建并填充数据到上面提到的JS对象名:person; 将它格式化

自从上两天以来,我一直在尝试JavaScript和PHP之间的通信。在阅读了大量关于其他人遇到的类似问题的教程、建议和解决方案后,我还无法如愿成功。可以说,我是在我的一个域上编程,而不是在本地

目标是: 从JavaScript中格式良好的对象向我的电子邮件地址发送一个格式非常好的字符串。为了做到这一点,我知道需要使用PHP脚本著名的电子邮件功能来执行它

为实现这一目标采取的步骤: 有一个功能正常的JavaScript,没有错误,可以创建并填充数据到上面提到的JS对象名:person; 将它格式化成一个好的字符串,因为我希望它以纯文本格式而不是HTML格式发送; 让jQuery的$.ajax调用与服务器端进行交互,服务器端是一个PHP,包含必要的头和代码,用于将简单邮件发送到指定的目的地,例如我的电子邮件地址。 可选的是经过了GZIP压缩,缩小了HTML/CSS/JS,以便更快地将内容加载到客户端。 有什么问题吗? 我的AJAX代码运行平稳,没有错误,但没有达到预期的效果。它不是跳转/进入成功,而是直接进入错误定制函数,我似乎无法解释可能出现的错误

我将在下面发布JS AJAX调用和我用来实现上述目标的PHP脚本

JavaScript更新:

PHP脚本已更新:

使现代化
$.ajax根本没有必要,而是开始使用普通的JS和XMLHttpRequest方法。内容成功发送到我的PHP文件,但通过检查邮件是否为true/false sent/not,结果为false。需要一个配置文件向PHP提供SMTP设置的信息。我使用的是Arvixe主机,其中安装了IIS服务器。我对web托管和维护域有点陌生,因此我不知道是应该在我的项目文件夹中配置web.config文件,还是添加扩展PHP配置文件。

我认为您需要在答案中添加成功的标题和相关代码200

如果您的答案是json格式的jquery,请读取该代码以重定向到成功或错误处理程序

检查php函数的标题:


告诉我是否有效

经过几天的研究,我终于找到了一个可行且非常有效的解决方案。问题在于,我的域设置为在Windows Server 2008+上运行,IIS托管,因此PHP的邮件功能不完整,无法执行邮件

解决方案过去是,现在是:PHPMailer是一个非常常见且使用率最高的php库,专门设计用于向开发人员提供自信且简单的代码。我已经从源代码中下载了它作为zip文件,然后将其解压缩。之后,创建了一个空白的新php文件,并复制/粘贴了如何发送邮件的演示脚本,并根据我的需要进行了修改。最后将PHPMailer文件夹和新创建的脚本上载到我的域。手动执行脚本后,我在收件箱中收到了邮件,而不是垃圾邮件。这 也救了我一命。 脚本如下所示:

<? 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';
        }
    } 
?>

我看不到您从哪里获得$\u POST['some']data$postvars=file\u get\u contents'php://input'; 类似于使用$\u POST['some]。如果我不得不使用后者,那么我就必须在ajax的“data”属性中使用对象,而不是数组。试着这样做,然后打印出来进行调试。如果您可以获得适当的数据,那么您解析的方式就有问题。在JSON Plus中使用此选项:错误:onFailure}时是否缺少分号?缺少分号:true。现在更新。如何实现将php中解析的值打印到控制台或DOM元素中的任何位置?我尝试了您的精确代码。我不得不注释掉邮件功能,因为它在我的电脑上没有很好的配置,所以它给了我一个错误,但在这样做之后,我没有得到任何错误。您的问题当然是邮件抛出了一个错误,从而发送了500个错误代码或使输出不是有效的JSON,因此jQuery认为这是一个错误。我也研究了这个问题。但问题是,如果它在没有OK响应头的情况下工作,它将在第一行发送一封电子邮件。所以我想问题不在这里。据我所知,问题在于ajax中的contentType/dataType与PHP中提到的标题不符。尝试在failure或success处理程序中检查数据变量,并在json答案中查找一些问题;记住检查邮件功能的成功或失败以发送响应。
<?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();
?>
<? 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';
        }
    } 
?>