我应该如何正确构建这个PHP邮件脚本?

时间:2016-08-23 07:04:32

标签: javascript php ajax email

我试图通过PHP& amp;将收集到的html表单的值作为电子邮件发送给我自己。阿贾克斯。出于某种原因,我可以使用成功提醒来更新UI,但是当我检查我的收件箱时,没有发送实际的电子邮件。我的印象是我的PHP脚本可能结构不合理,因为当我记录我的js函数的结果时,所有表单值都已正确捕获。

这是JS:

function _(id){ return document.getElementById(id); };
function submitForm(){
    var formdata = new FormData();
    formdata.append( "first-name", _("first-name").value );
    formdata.append( "last-name", _("last-name").value );
    formdata.append( "email", _("email").value );
  formdata.append( "subject", _("subject").value );
  formdata.append( "message", _("message").value );
    var ajax = new XMLHttpRequest();
    ajax.open( "POST", "email_me.php" );
    ajax.onreadystatechange = function() {
        if(ajax.readyState == 4 && ajax.status == 200) {
            if(ajax.responseText == "success"){
                alert("Hey! It Worked!!");
            } else {
            // display error
            }
        }
    }
    ajax.send( formdata );
  // Display the key/value pairs
  for (var pair of formdata.entries()) {
      console.log(pair[0]+ ', ' + pair[1]);
  }
}

这是php脚本(email_me.php文件)

<?php
  if(isset($_POST['first-name'], $_POST['last-name'], $_POST['email'], $_POST['subject'], $_POST['message'])){
    $name = $_POST['first-name'];
    $email = $_POST['email'];
    $m = nl2br($_POST['message']);
    $to = "me@gmail.com";
    $from = $email;
    $subject = $_POST['subject'];
    $message = '<p>'.$m.'</p>';
    $headers = "From: $from\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\n";
    if( mail($to, $subject, $message, $headers) ){
        echo "success";
    } else {
        echo "The server failed to send the message. Please try again later.";
    }
  }
?>

问题似乎是什么?我用本地主机运行当前版本的Apache,使用MAMP Pro btw。

以下是服务器日志:

Marker - 2016年8月23日,下午12:34:32

  

Aug 23 12:35:24 MacBookAir postfix / master [7884]:守护进程启动 - 版本2.11.0,配置/ etc / postfix   8月23日12:36:24 MacBookAir postfix / master [7884]:主出口时间到了

     

Aug 23 12:36:24 MacBookAir postfix / master [7885]:守护进程启动 - 版本2.11.0,配置/ etc / postfix   8月23日12:37:24 MacBookAir postfix / master [7885]:主出口时间已到   8月23日12:37:24 MacBookAir r postfix / master [7886]:守护进程启动 - 版本2.11.0,配置/ etc / postfix

1 个答案:

答案 0 :(得分:0)

更新你的PHP代码并检查它是否可行

 if(isset($_POST['first-name'], $_POST['last-name'], $_POST['email'], $_POST['subject'], $_POST['message'])){
    $name = $_POST['first-name'];
    $email = $_POST['email'];
    $m = nl2br($_POST['message']);
    $message = '<p>Name => '.$name.' <br/> Email =>'.$email.'<br /> Message =>'.$m.'</p>';
    $to = "me@gmail.com";

    $subject = $_POST['subject'];
    $headers = "From: noreply@something.com\r\n"; // use \r\n
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";  // use \r\n
    $headers.= "X-Priority: 1\r\n";
    if( mail($to, $subject, $message, $headers) ){
        echo "success";
    } else {
        echo "The server failed to send the message. Please try again later.";
    }
  }