0

我正在尝试使用 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;
}
?>

我的错误:无法发送消息。邮件程序错误:SMTP 连接()失败。https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

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

4

2 回答 2

3

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

告诉我它对您有用,如果没有,请确保在您的帐户设置中启用“安全性较低的应用程序”,谢谢!

让我知道它对你有用吗?

于 2018-07-11T15:51:38.997 回答
0

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

于 2019-03-06T07:17:57.220 回答