简体   繁体   English

Mailer错误:SMTP connect()失败?

[英]Mailer Error: SMTP connect() failed?

I am facing a problem regarding SMTP connection in php mailer, Here is my code 我在php mailer中遇到有关SMTP连接的问题,这是我的代码

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function

//Load composer's autoloader
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'Exception.php';
require 'PHPMailer.php';
require 'SMTP.php';
$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings

    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'mine@gmail.com';                 // SMTP username
    $mail->Password = 'password';                           // SMTP password
    $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('mine@gmail.com', 'mine');
    $mail->addAddress('mine@gmail.com', 'mine');     // Add a recipient
    $mail->addReplyTo('mine@gmail.com', 'mine');
    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = $_POST['name'];
    $mail->Body    = $_POST['mail']."<br>" .$_POST['phn']."<br>".$_POST['des'];
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';echo "<br>";`enter code here`
    echo "<a href=index.html>Go Back";
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}

?>

Actually this code was running perfectly and i was getting mail but now it is not working and saying Mailer Error: SMTP connect() failed this as an error. 实际上,这段代码运行得很好,我正在接收邮件,但现在却无法正常工作,并说Mailer错误:SMTP connect()将其作为错误失败 Help me !!! 帮我 !!!

Thanks... 谢谢...

Try it this way... maybe the certificate is selfsigned or something like this. 以这种方式尝试...也许证书是自签名的或类似的东西。

Failing that, 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类子类化来实现此目的),尽管不建议这样做,因为这样做会破坏使用安全性的许多要点。运输:

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

https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting https://github.com/PHPMailer/PHPMailer/wiki/疑难解答

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

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