php 如何解决“SMTP 错误:无法连接到 SMTP 主机”。在 phpmailer 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30148524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:33:59  来源:igfitidea点击:

How to resolve 'SMTP Error: Could not connect to SMTP host.' in phpmailer?

phpphpmailer

提问by Rahul Singh

I'm trying to send email using my gmail account but i am getting an error

我正在尝试使用我的 gmail 帐户发送电子邮件,但出现错误

"SMTP Error: Could not connect to SMTP host".

“SMTP 错误:无法连接到 SMTP 主机”。

I have tried port 587,465 25 but still its not working.

我尝试了端口 587,465 25 但仍然无法正常工作。

<?php
if(isset($_POST['submit']))
{

    $message=
        'Full Name: '.$_POST['fullname'].'<br />
        Subject:    '.$_POST['subject'].'<br />
        Phone:  '.$_POST['phone'].'<br />
        Email:  '.$_POST['emailid'].'<br />
        Comments:   '.$_POST['comments'].'
       ';
require "phpmailer/class.phpmailer.php"; //include phpmailer class

// Instantiate Class
$mail = new PHPMailer();

// Set up SMTP
$mail->IsSMTP();                // Sets up a SMTP connection
$mail->SMTPAuth = true;         // Connection with the SMTP does require authorization
$mail->SMTPSecure = "ssl";      // Connect using a TLS connection
$mail->Host = "smtp.gmail.com";  //Gmail SMTP server address
$mail->Port = 465;  //Gmail SMTP port
$mail->SMTPSecure = "tls";

$mail->Encoding = '7bit';

// Authentication
$mail->Username   = "[email protected]"; // Your full Gmail address
$mail->Password   = "*********"; // Your Gmail password

// Compose
$mail->SetFrom($_POST['emailid'], $_POST['fullname']);
$mail->AddReplyTo($_POST['emailid'], $_POST['fullname']);
$mail->Subject = "New Contact Form Enquiry";      // Subject (which isn't required)
$mail->MsgHTML($message);

// Send To
$mail->AddAddress("[email protected]", "RahulName"); // Where to send it - Recipient
$result = $mail->Send();        // Send!
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);

}
?>
<html>
<head>
     <title>Contact Form</title>
</head>
<body>

    <div style="margin: 100px auto 0;width: 300px;">
        <h3>Contact Form</h3>
        <form name="form1" id="form1" action="" method="post">
                <fieldset>
                  <input type="text" name="fullname" placeholder="Full Name"    />
                  <br />
                  <input type="text" name="subject" placeholder="Subject" />
                  <br />
                  <input type="text" name="phone" placeholder="Phone" />
                  <br />
                  <input type="text" name="emailid" placeholder="Email" />
                  <br />
                  <textarea rows="4" cols="20" name="comments" placeholder="Comments"></textarea>
                  <br />
                  <input type="submit" name="submit" value="Send" />
                </fieldset>
        </form>
        <p><?php if(!empty($message)) echo $message; ?></p>
    </div>              
</body>
</html>

How can I solve this problem?

我怎么解决这个问题?

采纳答案by Abhinav

First of all, while initiating PHPMailer send in the parameter truelike,

首先,在启动 PHPMailer 时发送参数, true例如,

$mailer = new PHPMailer(true);

This will help you to catch and handle exceptions.

这将帮助您捕获和处理异常。

Secondly, try this

其次,试试这个

$mailer->SMTPSecure = 'ssl'; // instead of tls

回答by Synchro

TLS on port 465 will not work - that port expects SSL. You should change it to port 587. You should not change to ssl for encryption as it's been deprecated since 1998.

端口 465 上的 TLS 将不起作用 - 该端口需要 SSL。您应该将其更改为端口 587。您不应更改为 ssl 进行加密,因为它自 1998 年以来已被弃用。

It would also help if you read the docsand base your code on an up-to-date example.

如果您阅读文档根据最新示例编写代码,这也会有所帮助。

回答by Geovas

Try:

尝试:

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

works for me!

为我工作!