簡體   English   中英

SMTP 錯誤:無法連接到服務器:使用 phpmailer 時連接超時(110)

[英]SMTP ERROR: Failed to connect to server: Connection timed out (110) when using phpmailer

這在我的舊服務器中完美運行。

2020-07-18 16:37:45 SMTP 錯誤:無法連接到服務器:連接超時 (110) SMTP 連接()失敗。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting我現在正在使用 CentOS 7 和 php7.3.2

我試過的。

  • 使用端口 587,465,25
  • 使用主機 smtp.gmail.com,108.177.122.108

這是我的代碼:

<?php
require_once "config.php";
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $randompassword = generateRandomString();
    $email = $_POST['email'];
    $mail = new PHPMailer(true);
    try {
        $connect->query("SET NAMES 'utf8'");
        $insertSql = "INSERT INTO users (username, password, email, forgot) VALUES ('$name', '$randompassword', '$email', 1)";
        $status = $connect->query($insertSql);
        $mail->isSMTP();                                            // Send using SMTP
        $mail->SMTPAuth   = true;
        $mail->Host       = 'smtp.gmail.com';                    // Set the SMTP server to send through
        $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
        $mail->Username   = 'username@gmail.com';                     // SMTP username
        $mail->Password   = '*******';                               // SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
        $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
        $mail->Charset    = "UTF-8";
        $mail->setFrom('username@gmail.com', 'Mailer');
        $mail->addAddress($email);               // Name is optional
        $mail->addReplyTo('to@gmail.com', 'to');                                 // Set email format to HTML
        $mail->Subject = 'subject';
        $mail->Body    = 'body'
        $mail->send();
        echo '<script>alert("already send");</script>'; 
    } 
    catch (Exception $e) {
        echo "<script>alert('error');</script>";
        echo !extension_loaded('openssl')?"Not Available":"Available";
        exit;
    }
}
?>

更新

我測試了端口 25 465 587 的 telnet smtp.gmail.com,結果也是正常的連接超時。

我正在使用 Linode 服務器,他們阻止了端口 25、465、587,所以我無法發送郵件和遠程登錄 smtp.gmail.Z4D236D9A2D102C50FE6AD1C5ZDAB4。 如需更多信息,請點擊此處。

網絡問題。 防火牆正在阻止這些端口。 令人驚訝的是它也阻塞了 SSL 端口。 一旦你解決了這個問題,你應該對 go 很好。 作為替代方案,您可以將 SMTP 服務器上的端口更改為您的提供商允許您使用的自定義端口(25000、25252 等)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM