PHPMailer / PHPMailer

The classic email sending library for PHP
GNU Lesser General Public License v2.1
21.05k stars 9.74k forks source link

Version 6 - SMTP ERROR: Failed to connect to server: (0) #1347

Closed alexsdesign closed 6 years ago

alexsdesign commented 6 years ago

Using the latest version of PHP Mailer 6 ... downloaded by ZIP to desktop and uploaded to server. Attempted this on TWO servers: Windows 2016 and Centos 6 (Plesk Onyx).

File Structure: --- Root Folder ------ [ PHPMailer ] --------- get_oauth_token.php --------------- [ src ]
------------------ ------- Exception.php
------------------ ------- OAuth.php
------------------ ------- PHPMailer.php
------------------ ------- POP3.php
------------------ ------- SMTP.php

Here is the example code I'm using with "personal" information faked (such as email address and password.

<?php

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

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

//Create a new PHPMailer instance
$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = gethostbyname('mail.domain.com'); // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'myemail@domain.com';                 // SMTP username
    $mail->Password = 'Password123#';                           // SMTP password
    $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('myemail@domain.com', 'Alex');
    $mail->addAddress('someoneelse@domain2.com', 'John');     // Add a recipient
    //$mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('anotherone@domain3.com', 'Test');
    $mail->addCC('test@hotmail.com');
    $mail->addBCC('myemail2@domain.co');

    //Attachments
    //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //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 '<br><br>Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}?>

I'm using the basic example code provided by you guys but every time I run it, I get the following error:

2018-01-31 21:13:03 SMTP ERROR: Failed to connect to server: (0)
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

I ran a check to see if Open SSL is loaded: <?php echo (extension_loaded('openssl')?'SSL loaded':'SSL not loaded')."\n"; ?> I can confirm that Open SSL is loaded on the server/domain and the domain is SSL enabled.

Apologies if this is a known issue with a known fix ( Or just my shoddy coding )... I have searched but nothing comes up for Version 6. If any more information is required please let me know.

halfer commented 6 years ago

Can you turn on SMTP debugging in PHPMailer, grab the logs, and post them here, so we can take a look?

(Tip: use the code formatting tool in the editor title bar to render code. Please make that change now, so that your code is readable for current and future readers).

alexsdesign commented 6 years ago

The code was pasted inbetween the code "quote marks" so I don't know why that occurred, apologies.

SMTP Debugging is turned on... and set to 2 ... client and server side. $mail->SMTPDebug = 2;

The total response is:

2018-01-31 21:13:03 SMTP ERROR: Failed to connect to server: (0) 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

With SMTPDebug turned off ( set to 0 ) I get the following: Message could not be sent. Mailer Error: SMTP connect() failed. Https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Synchro commented 6 years ago

You're not getting any SMTP debug output because it's not getting as far as talking SMTP - it's failing at the network level. I'd guess that your ISP is blocking outbound SMTP. The telnet tests in the troubleshooting guide will confirm that. You probably need to refer to their docs or ask them to enable SMTP.

alexsdesign commented 6 years ago

The code is running on my own colocated server. I'm currently running phpMailer 5 on a seperate domain (on the same server) and that connects with no issue.

I'll do some more testing and digging and see what I can work out. (The current setup is not using the composer built version, so I might try that next)

arnabmunshi commented 6 years ago

2018-02-05 02:38:08 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP r3sm10281083pgf.46 - gsmtp 2018-02-05 02:38:08 CLIENT -> SERVER: EHLO localhost 2018-02-05 02:38:08 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [103.77.46.221]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8 2018-02-05 02:38:08 CLIENT -> SERVER: STARTTLS 2018-02-05 02:38:08 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS SMTP Error: Could not connect to SMTP host. 2018-02-05 02:38:08 CLIENT -> SERVER: QUIT 2018-02-05 02:38:08 2018-02-05 02:38:08 SMTP Error: Could not connect to SMTP host. Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.

krystyna93 commented 6 years ago

@arnabmunshi did you work out what the issue was? Thanks.

Synchro commented 6 years ago

You've got a TLS failure after attempting STARTTLS, and you're connecting to gmail, which changed its root cert not so long ago to one that's not in many OS CA cert bundles. That's all covered in the troubleshooting guide - so try updating your local CA certs.