php – PHPMailer Can't Connect, but off-server SMTP testing tool works-ThrowExceptions

Exception or error:

I’m trying to implement a mailer in my PHP website.

Using PHPMailer installed with Composer. I’m on Ubuntu 18.04 LTS on a Linode VPS.

The page takes a very long time to load, then shows the exception.


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'vendor/phpmailer/phpmailer/src/Exception.php';
require 'vendor/phpmailer/phpmailer/src/PHPMailer.php';
require 'vendor/phpmailer/phpmailer/src/SMTP.php';

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = '';                    // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = '';                     // 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::ENCRYPTI$
    $mail->setFrom('', 'The PC Repair Network');
    $mail->addAddress('', '');     // Add a recipient
    $mail->addReplyTo('', '');

    // Attachments

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";


2020-03-16 05:13:59 SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed.
Message could not be sent. Mailer Error: SMTP connect() failed.

SMTP Server forwards successfully to

SMTP Ports:  465 (SSL), 587 (STARTTLS), 25 (Non Encrypted)
Force SSL:  True

What I’ve tried:

  • Allowing outbound connections on port 587 with sudo ufw allow out 587

  • Testing the SMTP server with which returns successfully

  • Testing the SMTP server with Windows-based “SMTP Diag Tool” which successfully sent an email

  • Testing with telnet from my server, which hangs on Trying **ipaddress**...

I have a feeling whatever is causing it not to work is also causing my telnet command not to resolve.


I’ve opened a support ticket with Linode after reading that they have a policy of closing ports 25, 465, and 587 for new customers until CAN-SPAM compliance is ensured.

How to solve:

Per Linode’s user agreement, outbound mail ports are blocked by default. A support ticket later and I’m up and running. If you run into an issue with PHPMailer or SwiftMailer like the above (notably the extreme page load times), then confirm that your hosting provider is not blocking ports 465, 25, and 587.

Leave a Reply

Your email address will not be published. Required fields are marked *