SMTP错误:无法连接到SMTP主机,使用Gmail发送邮件

时间:2018-02-01 11:55:32

标签: php smtp

邮件发送功能在QA服务器(本地IP)中不起作用。下面给出了代码片段

public function Sendmail($id) {

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

 $mail=New PHPMailer();    
$subject = "Action Items";
$content = "<b>Dear All,</b>";
$content .= "my msg";

$content .="<br>";
$content .="Thanks and regards";
$content .="<br>";
$content .= $this->view->getactionitems[0]['Assinedby'];


$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "ssl";
$mail->Port     = 465;  
$mail->Username = "test@gmail.com";
$mail->Password = "pass$";
$mail->Host     = "smtp.gmail.com";
$mail->Mailer   = "smtp";
$mail->SetFrom("abc@gmail.com", "Shanu");
$mail->AddReplyTo("abc@gmail.com", "Shanu");
$mail->AddAddress("user@gmail.com");

$mail->Subject = $subject;
$mail->WordWrap   = 80;
$mail->MsgHTML($content);
$mail->IsHTML(true);

if(!$mail->Send()) 
     $this->view->status = 'invalid';

else 
 $this->view->status = 'success';
     $this->view->renderAdmin('meetings/meetings');   
    }  

它已在开发PC中成功测试。错误消息如下所示

  

SMTP错误:无法连接到SMTP主机

我启用了extension=php_openssl.dll。但同样的错误。感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

如果您的服务器没有SSL,并且您尝试发送邮件,则可能会出现此错误。

所以对于PHP你可以做到

$mail->SMTPOptions = array(
   'ssl' => array(
       'verify_peer' => false,
       'verify_peer_name' => false,
       'allow_self_signed' => true
     )
  );
  

SSL验证对等方使其 false more

希望这会有所帮助。