PHPMailer不工作

时间:2018-02-14 11:13:16

标签: php phpmailer

我尽力使用phpmailer在gmail帐户上收到邮件,但我不能。它显示消息已成功发送但我没有收到任何消息。我甚至编辑了sendmail.ini文件。这是代码:

use PHPMailer\PHPMailer\PHPMailer;    
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);   

//$mail->SMTPDebug = 2;           
//$mail->isSMTP();           
$mail->Host = 'smtp.gmail.com';    
$mail->SMTPAuth = true;          
$mail->Username = 'myemail@gmail.com';              
$mail->Password = 'mypassword';     
$mail->SMTPSecure = 'ssl';     
$mail->Port = 587;          
$mail->setFrom('myemail@gmail.com', 'Mailer');
$mail->addAddress('example@gmail.com', 'Joe User');     

$mail->isHTML(true);                
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';

if($mail->send()){
echo 'Message has been sent successfully';
} else {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

我没有回答这么多问题,但我仍然需要帮助

3 个答案:

答案 0 :(得分:1)

setFrom更改为SetFrom,然后尝试将IP更改为465。 您也可以尝试以下方法。

$mail = new PHPMailer();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "email@gmail.com";
$mail->Password = "password";
$mail->SetFrom("example@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email@gmail.com");

if($mail->send()){
    echo 'Message has been sent successfully';
} else {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

如果仍然出现错误,请提供错误消息。

答案 1 :(得分:0)

尝试:

$mail->SMTPSecure = 'tls';     
$mail->Port = 587;  

希望这会有所帮助。

答案 2 :(得分:-1)

首先,尝试取消注释行

  

$ MAIL-&GT; isSMTP();

如果您通过https发送邮件,请尝试添加以下代码:

  

$ mail-&gt; SMTPOptions = array('ssl'=&gt; array(                 'verify_peer'=&gt;假,                 'verify_peer_name'=&gt;假,                 'allow_self_signed'=&gt;真));