使用PHPMailer发送邮件不起作用

时间:2013-10-30 21:36:07

标签: php email phpmailer

所以我正在尝试使用PHPMailer处理我网站上的电子邮件表单。

我根据我找到的教程在这里编写了代码。

<?php
        error_reporting(E_ALL);
        require_once("class.phpmailer.php");
        include("class.smtp.php");

        $email = new PHPMailer();

        //
        // Set server details for send
        //
        $email->IsSMTP();
        $email->Host = "mail.loganyoung.za.net";
        $email->Port = 25;
        $email-SMTPAuth = true;
        $email->Username = "<my email>";
        $email->Password = "<my password>";

        //
        // Send mail from the contact form
        //
        $to = "<my email>";
        $from = $_POST["from"];
        $name = $_POST["name"];
        $subject = "From web: ".$_POST["subject"];
        $message = $_POST["message"];

        $body = "<p>Hi Logan,</p>";
        $body .= "<p>You have received a new query on your website.<br />Please see below:</p>";
        $body .= "<p>";
        $body .= str_replace("\r\n", "<br />", $message);
        $body .= "</p>";

        $email->SetFrom($from, $name);
        $email->AddReplyTo($from, $name);
        $email->AddAddress($to, "LoganYoung.za.net");
        $email->Subject = $subject;
        $email->Body = $body;
        $email->IsHTML = true;

        session_start();
        if(!$email->Send()) {          
                $_SESSION["mailresult"] = "success";
                echo "success";
        } else {
                echo "<p>Failed:</p><p>".$email->ErrorInfo."</p>";
                $_SESSION["mailresult"] = "failed";
                $_SESSION["mailerror"] = $email->ErrorInfo;
        }

?>

我认为不发送的可能原因可能是......

  • 不正确的SMTP详细信息(可能在没有SMTP身份验证的情况下发送以解决?)
  • 处理程序没有获取POST数据(发送它的ajax似乎工作正常)
  • 此代码存在一些我无法识别的问题......

作为消除可能性的一种方法,任何人都可以发现代码中的任何错误吗?如果是这样,那有什么问题,我该如何解决?

提前致谢!

1 个答案:

答案 0 :(得分:1)

$email-SMTPAuth = true;

不应该是:

$email->SMTPAuth = true;