PHP 邮件程序在尝试发送邮件时在 php 7 中抛出错误

PHP mailer throws error in php 7 when tried send mail

我正在尝试使用 PHP Mailer 发送邮件,但出现错误。 请检查我的代码让我知道修复。

这是我的代码:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

$mail = new PHPMailer;

try {
    //Server settings
   $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'xxxxxxxxxxxx ';
    $mail->Password = 'xxxxxxxxx';
    //$mail->SMTPSecure = 'tls';                          // Enable TLS encryption, `ssl` also accepted
    $mail->SMTPSecure = 'ssl';                          // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('xxxxxxx', 'shiva');

    $mail->addAddress('siva.sing.sivan@gmail.com', 'SP');     // Add a recipient
    $mail->addAddress('senthil.mca2008@gmail.com', 'SK');     // Add a recipient
    //\$mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('opensourcesivaprakash@gmail.com', 'Information');


    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Test mail form SP';
    $mail->Body    = 'This is the HTML message  <b>From SP!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

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


} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>

我的错误: 消息不能 sent.Mailer 错误:SMTP connect() 失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

请更正我的代码以解决问题。

嘿@shiva 使用此 $mail->SMTPSecure = 'tls' 并将端口用作 $mail->Port = 587 就是这样!

告诉我它对你有用,如果不行,请确保在你的帐户设置中启用“不太安全的应用程序”,谢谢!

让我知道它对你有用吗?

将 php 版本升级到 7.2.x 并尝试内置 openssl 1.1.1