使用 phpmailer 库的 smtp connect() 在 PHP 中失败

smtp connect() failed in PHP using phpmailer Library

我发现在 PHP 中使用 phpmailer Library.any 发送邮件时出错 Library.any 可以回答这个错误。 谢谢。

smtp connect() 失败。 https://github.com/phpmailer/phpmailer/wiki/troubleshooting

require_once APPPATH.'third_party/phpmailer/class.phpmailer.php';
require_once APPPATH.'third_party/phpmailer/class.smtp.php';

    $rtn = false;
    $mail = new PHPMailer();

    $mail->IsSMTP();
    $mail->SMTPDebug  = 0;
    $mail->SMTPAuth   = true;
    $mail->SMTPSecure = "tls";
    $mail->Host       = 'smtp.gmail.com';
    $mail->Port       = 587;
    $mail->Username   = 'myemail@gmail.com';
    $mail->Password   = 'mypassword';
    $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );
    $mail->AddAddress($email);
    $mail->SetFrom('myemail@gmail.com', 'Smart Saver Support');
    $mail->AddReplyTo('myemail@gmail.com', 'Smart Saver Support');
    $mail->IsHTML(true);
    $mail->Subject = $subject;
    $mail->MsgHTML($msg);

    $rtn = $mail->Send();
    return $rtn;

可能是SMTP认证时输入的用户名密码错误或 SMTP 端口在邮件服务器防火墙中被阻止。

如果您使用的是 Gmail 帐户,请选中此 link 并关闭 "Allow less secure apps"。

这是 URL: https://myaccount.google.com/lesssecureapps

然后尝试再次发送电子邮件。

嘿,你 Allow less secure apps to access your Gmail account 吗? 如果您不允许安全性较低的应用再次启用并 运行 并查看结果。

示例如下: https://devanswers.co/allow-less-secure-apps-access-gmail-account/