PHPMailer中的致命错误

时间:2014-10-09 02:14:22

标签: php phpmailer

这里的初学者。

我已经检查了论坛以找到答案,但我没有成功,其他问题专门针对PHPMailer的某些部分,但我的更为一般。所以我希望没有人会像我在学习曲线那样将我的问题标记为重复。

我正在开发一个PHP项目。它的工作原理是用户进入页面并在表单中写入一些注释或问题(如文本编辑器)并单击发送按钮。我应该能够收到他的电子邮件。我在此处设置了我的Gmail帐户以用于测试目的,但稍后它将是我自己的域名的真实电子邮件。

以下是我在本地主机上运行时收到的错误:

  

致命错误:未捕获的异常' phpmailerException'有消息'无法执行:/ usr / sbin / sendmail'在C:\ xampp \ htdocs \ pp \ classes \ class.phpmailer.php:1100堆栈跟踪:#0 C:\ xampp \ htdocs \ pp \ classes \ class.phpmailer.php(1026):PHPMailer-> sendmailSend( '日期:星期四,星期五...',' - b1_9ea0b33e3f ...')#1 C:\ xampp \ htdocs \ pp \ classes \ class.phpmailer。 php(935):PHPMailer-> postSend()#2

以下是我正在使用的代码:

<?php 
require_once("../../classes/class.phpmailer.php");

if($_POST['mode']=='send'){

    $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch

    $mail->IsSendmail(); // telling the class to use SendMail transport

        //I assume this part is to make it run on linux base on Google search
    $body = "New Bug Report from ".$_SESSION['name']."\n".$_POST['bug'];
    $mail->AddReplyTo('mj@gmail.com', 'MJ Team');
    $mail->AddAddress(''.$_SESSION['email'].'', ''.$_SESSION['name'].'');
    $mail->SetFrom(mj@gmail.com', 'MJ Team');
    $mail->AddReplyTo('mj@gmail.com', 'MJ Team');
    $mail->Subject = 'New bug report for the portal';
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
    $mail->MsgHTML($body);
    $mail->Send();

            //And I assume this part of the code makes it run on windows based on Google search
        $mail->IsSMTP();
        $mail->SMTPAuth = true;
        $mail->Host = "smtp.postmarkapp.com";
        $mail->Port = 26;
        $mail->Username = "MJ";
        $mail->Password = "MJ";
        $mail->SetFrom('mj@gmail.com', 'mj');
        $mail->Subject = "An email for test";
        $mail->AddAddress($address, $name);

    if($mail){
        $message = 'Thanks. Bug report successfully sent. We will get in touch if we have any more questions.';
        }
        else {
            echo "Mailer Error: " . $mail->ErrorInfo;
        }
}
?>

正如额外信息一样,我无法找到任何用户并通过SMTP,所以我只是填写了我的名字,这显然不应该是正确的。 由于我是初学者,我感谢任何可能帮助我运行代码的评论和建议代码。

谢谢!

2 个答案:

答案 0 :(得分:0)

试试这个:

<?php 
session_start();

if(isset($_POST['mode']) && $_POST['mode']=='send' && 
   isset($_SESSION['email'], $_SESSION['name'])){

    require_once("PHPMailerAutoload.php");
    $mail = new PHPMailer(true);

    //Send mail using gmail
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->SMTPAuth = true; // enable SMTP authentication
    $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
    $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
    $mail->Port = 465; // set the SMTP port for the GMAIL server
    $mail->Username = "mj@gmail.com"; // GMAIL username
    $mail->Password = "mypassword"; // GMAIL password

    //Typical mail data
    $mail->AddAddress($_SESSION['email'], $_SESSION['name']);
    $mail->SetFrom('mj@gmail.com', 'mj');
    $mail->Subject = "This is a test message";
    $mail->Body = "An email for test";

    try{
        $mail->Send();
        echo "Thanks. Bug report successfully sent.
              We will get in touch if we have any more questions!";
    } catch(Exception $e){
        //Something went bad
        echo "Mailer Error: - " . $mail->ErrorInfo;
    }
}else{
    echo 'missing required values!';
}

答案 1 :(得分:-1)

如果您只想发送电子邮件,为什么不使用PHP Mail命令(http://php.net/manual/en/function.mail.php

用以下代码替换您的代码:

$to      = 'mj@gmail.com';
$subject = 'the subject';
$message = 'New bug report for the portal';
$headers = 'From: mj@gmail.com' . "\r\n" .
    'Reply-To: mj@gmail.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);