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

时间:2015-05-10 06:49:11

标签: php phpmailer

我尝试使用我的Gmail帐户发送电子邮件,但我收到错误

  

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

我尝试过端口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   = "rhlsngh302@gmail.com"; // 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("rhlsngh302@gmail.com", "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>

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:2)

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

如果您read the docs并将代码基于up-to-date example,这也会有所帮助。

答案 1 :(得分:1)

尝试:

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

适合我!

答案 2 :(得分:0)

首先,在启动PHPMailer时发送参数true之类的,

$mailer = new PHPMailer(true);

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

其次,试试这个

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