简体   繁体   English

使用Gmail smtp服务器发送邮件时,PHPMailer给出错误

[英]PHPMailer gives an error when sending mails with the Gmail smtp server

I'm using phpmailer in a selfmade template/mail function: 我在自制模板/邮件功能中使用phpmailer:

<?php  
require_once(__DIR__.'/../../vendor/phpmailer/phpmailer/PHPMailerAutoload.php');
function sendTemplateMail($body, $data, $subject, $receiver, $sender){
$template = Timber::compile($body, $data);
sendMail($template, $subject, $receiver, $sender);
}  

function sendMail($template, $subject, $receiver, $sender){

$mail = new PHPMailer(true);                        // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP     // TCP port to connect to

$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "**";
$mail->Password = "**";

$mail->setFrom($sender);
$mail->addAddress($receiver);     // Add a recipient
$mail->addReplyTo($sender);

$mail->isHTML(true);

$mail->Subject = $subject;
$mail->Body = $template;
$mail->AltBody = $template; //$mail->AltBody = $template; //

return $mail->send();
}

In my project the username and password are correct because they worked before. 在我的项目中,用户名和密码是正确的,因为它们以前曾起作用。 But now I get an error that the smtp is not connected. 但是,现在我得到一个错误,即未连接smtp。 fatal error: Uncaught phpmailerException: SMTP Error: Could not connect to SMTP host. 致命错误:找不到phpmailerException:SMTP错误:无法连接到SMTP主机。

Try this, add the sentence to sendMail function: 试试这个,添加句子到sendMail函数:

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

You can allow insecure connections via the SMTPOptions property introduced in PHPMailer 5.2.10 (it's possible to do this by subclassing the SMTP class in earlier versions), though this is not recommended as it defeats much of the point of using a secure transport at all. 您可以通过PHPMailer 5.2.10中引入的SMTPOptions属性允许不安全的连接(可以通过对早期版本中的SMTP类进行子类化来实现此目的), 尽管不建议这样做, 因为这样做会彻底破坏使用安全传输的大部分功能。

https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#php-56-certificate-verification-failure https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#php-56-certificate-verification-failure

This should be a temporary solution. 这应该是一个临时解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM