Skip to content
Advertisement

PHPMailer Can’t Connect, but off-server SMTP testing tool works

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.

test.php

<?
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;

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       = 'mail.thenetwork.com';                    // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'service@thenetwork.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::ENCRYPTI$
    //Recipients
    $mail->setFrom('service@thenetwork.com', 'The PC Repair Network');
    $mail->addAddress('myemail@gmail.com', '');     // Add a recipient
    $mail->addReplyTo('biz@thenetwork.com', '');

    // 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';

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

Exception

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

SMTP Server

mail.thenetwork.com forwards successfully to 
arrow.mxrouting.net

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 https://mxtoolbox.com/ 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.

Edit

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.

Advertisement

Answer

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.

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