PHPMailer不起作用,页面变白

时间:2017-07-14 14:20:09

标签: php jquery ajax phpmailer

我试图用PHPMailer发送电子邮件,但每次按下发送时,页面都会变白。我试图在error_log中搜索但内部没有任何内容。顺便说一下,我使用ajax,所有的插件都包含在内并且正在工作。

这是我的代码:

$.ajax({
            type: "POST",
            url: "cod_ajax/enviar_email.php",
            data: "nome="+nome+"&last="+last+"&email="+email+"&assunto="+assunto+"&texto="+texto,
            success:function(e){
                document.write(e);
                $("#envia_email").prop("disabled", true);
              setInterval(function(){
                $("#envia_email").html("Email Enviado com sucesso! Recarregue a página para enviar outro!");
              }, 2000);
                $(".erros").fadeOut(0);
                $("#name").val("");
                $("#last").val("");
                $("#email").val("");
                $("#assunto").val("");
                $("#texto").val("");
            }
        })

所有变量都是正确的,没有什么是空的并且很好地接收信息。这是我的ajax文件:

if(isset($_POST['name'])){
$nome=$_POST['nome'];
$last=$_POST['last'];
$email=$_POST['email'];
$assunto=$_POST['assunto'];
$texto=$_POST['texto'];

require '../mail/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->setFrom('b_daniel_18@hotmail.com');
$mail->addAddress('b_sem_l@hotmail.com');
$mail->Subject  = 'First PHPMailer Message';
$mail->Body     = 'Hi! This is my first e-mail sent through PHPMailer.';
if(!$mail->send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}
}

我没有使用这些变量,因为它没有用,所以我尝试手动添加信息,但仍然没有用。

更新

删除if(isset($_POST['name'])),我现在可以看到错误。它没有发送,出现 邮件程序错误:无法实例化邮件功能。

2 个答案:

答案 0 :(得分:0)

您在php文件中输入了错误的表单变量名称。变化

if(isset($_POST['name']))

要:

if(isset($_POST['nome']))

答案 1 :(得分:0)

试试此代码

      $mail = new PHPMailer();
        $mail->SMTPSecure = 'tls';
        $mail->Username = "b_daniel_18@hotmail.com";
        $mail->Password = "mypassword";
        $mail->AddAddress("b_sem_l@hotmail.com");
        $mail->FromName = "My Name";
        $mail->Subject = "My Subject";
        $mail->Body = "My Body";
        $mail->Host = "smtp.live.com";
        $mail->Port = 587;
        $mail->IsSMTP();
        $mail->SMTPAuth = true;
        $mail->From = $mail->Username;
        if($mail->Send()){
        echo 'Sent.<br/>';
        }else{
         echo 'Not sent: <pre>'.print_r(error_get_last(), true).'</pre>';
        }