PHPMailer无法发送电子邮件

时间:2014-12-04 21:39:25

标签: php email phpmailer

我正在创建PHP Class来发送电子邮件。问题是由于某种原因邮件没有发送。有人知道问题在哪里吗?

我在XAMPP上运行

以下是代码:

include_once "class.sendMAIL.php";
$sendActivation = new sendMAIL();
$body="http://$_SERVER[HTTP_HOST]/activation.php?id=$activationCode";
if ($sendActivation->sendActivationEmail($name, $email,$body))
{
    echo "MAIL SENT";
}
else {
    echo "ERROR IN SENDING";
}

邮件类:

require 'PHPMailerAutoload.php';
class sendMAIL {
    private $sendEmail;
    public function __construct() {
        $this->sendEmail =  new PHPMailer();
        $this->sendEmail->isSMTP();      // Set mailer to use SMTP
        $this->sendEmail->Host = ' smtp.gmail.com';  // Specify main and backup SMTP servers
        $this->sendEmail->SMTPAuth = true;    // Enable SMTP authentication
        $this->sendEmail->Username = 'my@gmail.com';    // SMTP username
        $this->sendEmail->Password = 'mypassword';           // SMTP password
        $this->sendEmail->SMTPSecure = 'ssl';     // Enable encryption, 'ssl' also accepted
        $this->sendEmail->port ="465";
        $this->sendEmail->From = "";
        //$this->sendEmail->FromName = $name;
        $this->sendEmail->isHTML(true);
    }

    public function sendActivationEmail($name, $email, $link) {
        $this->sendEmail->AddAddress($email);
        $this->sendEmail->Subject="Aktivacija";
        $body="Follow this link to activate your account: <a href='$link' />LINK</a>";
        $this->sendEmail->MsgHTML($body);
        if($this->sendEmail->send()) {
            return TRUE;
        }
        else {
            return FALSE;
        }       
    }
}

这是错误的,当我调试问题时我会发现:

You must provide at least one recipient email address.

感谢@paulgv我修复了错误,但现在我收到了这个错误:

SMTP connect() failed

有人知道问题在哪里吗?

编辑:问题导致gmail。 gmail阻止我的webapp访问我的电子邮件。

2 个答案:

答案 0 :(得分:1)

所以这是答案,从我之前发布的评论中复制出来;)

好吧,我的代码中也发现这句话很奇怪:$this->sendEmail->AddAddress=($email);可能会尝试使用$this->sendEmail->AddAddress($email);吗?

现在,关于您的SMTP身份验证问题,您是否尝试启用详细模式?可能有帮助 ! 根据文档,你应该做这样的事情:

$this->sendEmail->SMTPDebug = 3; 

答案 1 :(得分:0)

对于SMTP身份验证问题,请尝试以下操作:

$this->sendEmail->SMTPAuth   = true;                  // enable SMTP authentication
$this->sendEmail->SMTPSecure = "tls";                 // sets the prefix to the servier
$this->sendEmail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$this->sendEmail->Port       = 587;  

为我工作。