Skip to content
Advertisement

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

This works perfectly in my old server.

2020-07-18 16:37:45 SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting I am now using CentOS 7 and php7.3.2

What I’ve tried.

  • use port 587,465,25
  • use host smtp.gmail.com,108.177.122.108

This is my code:

<?php
require_once "config.php";
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;
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;
    }
}
?>

UPDATE

I test telnet smtp.gmail.com for port 25 465 587,the result is also connection time out.Then I test on my computer gets a normal result.

Advertisement

Answer

I’m using a Linode Server, they blocked port 25, 465, 587, so I can’t send mail and telnet smtp.gmail.com. for more infomation, ckick here.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement