Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting in php 7.4 #2868

Closed
Rignesh-Sarvaliya opened this issue Jan 30, 2023 · 15 comments

Comments

@Rignesh-Sarvaliya
Copy link

Rignesh-Sarvaliya commented Jan 30, 2023

require_once('PHPMailer/src/PHPMailer.php');
require_once('PHPMailer/src/SMTP.php');
require_once('PHPMailer/src/Exception.php');

        $subject = "Password Resert";
        $body = "Hi ";
        $sender_email = $_POST['email'];
        $mail = new \PHPMailer\PHPMailer\PHPMailer(true);
        try {
            //Server settings
            $mail->SMTPDebug = 0;                              // Enable verbose debug output
            $mail->isSMTP();                                   // Send using SMTP
            $mail->Host       = 'smtp.gmail.com';              // Set the SMTP server to send through
            $mail->SMTPAuth   = true;                          // Enable SMTP authentication
            $mail->Username   = '[email protected]';    // SMTP username
            $mail->Password   = 'password';            // SMTP password
            $mail->SMTPSecure = "ssl";                         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
            $mail->Port       = 465;                           // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
            //Recipients
             $mail->setFrom('[email protected]', 'project');
            $mail->addAddress('[email protected]');                  // Name is optional

            // Content
            $mail->isHTML(true);                               // Set email format to HTML
            $mail->Subject = $subject;
            $mail->Body    = $body;
            $mail->send();

        } catch (Exception $e) {
            print_r($e);
            exit;
        }
@Synchro
Copy link
Member

Synchro commented Jan 30, 2023

@Synchro Synchro closed this as completed Jan 30, 2023
@Rignesh-Sarvaliya
Copy link
Author

https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

I am using the latest PHPMailer version but again same error

@Synchro
Copy link
Member

Synchro commented Jan 31, 2023

Have you perhaps considered reading the docs that tell you how to diagnose and resolve this issue, that you have now been pointed at 3 times?

@Rignesh-Sarvaliya
Copy link
Author

Rignesh-Sarvaliya commented Jan 31, 2023

Have you perhaps considered reading the docs that tell you how to diagnose and resolve this issue, that you have now been pointed at 3 times?

I have tried all the ways but the issue is the same
Works in local but does not work in live

@Synchro
Copy link
Member

Synchro commented Jan 31, 2023

So why haven't you posted the results of your tests in here that show it's a problem with PHPMailer rather than your hosting? If your hosting provider blocks outbound SMTP (which is quite likely), there is nothing I can do. There is nothing I can suggest here that's not in the docs.

@Rignesh-Sarvaliya
Copy link
Author

Rignesh-Sarvaliya commented Jan 31, 2023

So why haven't you posted the results of your tests in here that show it's a problem with PHPMailer rather than your hosting? If your hosting provider blocks outbound SMTP (which is quite likely), there is nothing I can do. There is nothing I can suggest here that's not in the docs.

this error should be display from my testing

PHPMailer\PHPMailer\Exception Object ( [message:protected] => SMTP Error: Could not connect to SMTP host. Failed to connect to server [string:Exception:private] => [code:protected] => 0 [file:protected] => /home/uqx0dtuthzcf/public_html/mywebsite/PHPMailer/src/PHPMailer.php [line:protected] => 2233 [trace:Exception:private] => Array ( [0] => Array ( [file] => /home/uqx0dtuthzcf/public_html/mywebsite/PHPMailer/src/PHPMailer.php [line] => 2019 [function] => smtpConnect [class] => PHPMailer\PHPMailer\PHPMailer [type] => -> [args] => Array ( [0] => Array ( ) ) ) [1] => Array ( [file] => /home/uqx0dtuthzcf/public_html/mywebsite/PHPMailer/src/PHPMailer.php [line] => 1678 [function] => smtpSend [class] => PHPMailer\PHPMailer\PHPMailer [type] => -> [args] => Array ( [0] => Date: Tue, 31 Jan 2023 18:01:42 +0000 To: [email protected] From: Sumka Subject: Password Resert Message-ID: X-Mailer: PHPMailer 6.7.1 (https://github.com/PHPMailer/PHPMailer) MIME-Version: 1.0 Content-Type: text/html; charset=iso-8859-1 [1] => Hi, rignesh .[Click Here Too Reset Your Password](https://mywebsite.com/resetPassword.php?mid=2) ) ) [2] => Array ( [file] => /home/uqx0dtuthzcf/public_html/mywebsite/PHPMailer/src/PHPMailer.php [line] => 1516 [function] => postSend [class] => PHPMailer\PHPMailer\PHPMailer [type] => -> [args] => Array ( ) ) [3] => Array ( [file] => /home/uqx0dtuthzcf/public_html/mywebsite/recoverEmail.php [line] => 283 [function] => send [class] => PHPMailer\PHPMailer\PHPMailer [type] => -> [args] => Array ( ) ) ) [previous:Exception:private] => )

@Synchro
Copy link
Member

Synchro commented Jan 31, 2023

That's the error that shows that it's failing to connect, not tests that will show why. What happened when you tried to use the openssl command line client? What results do you get from DNS and connectivity tests?

@Rignesh-Sarvaliya
Copy link
Author

That's the error that shows that it's failing to connect, not tests that will show why. What happened when you tried to use the openssl command line client? What results do you get from DNS and connectivity tests?

This is a project in core-PHP and I work in Cpanel

@Synchro
Copy link
Member

Synchro commented Jan 31, 2023

PHP is PHP. That you're running it in a way that means you can't debug it is your own problem. Unfortunately, cpanel makes many things difficult and confusing. There's really nothing I can do if you don't understand your own hosting, and it has nothing to do with PHPMailer.

@Rignesh-Sarvaliya
Copy link
Author

Rignesh-Sarvaliya commented Jan 31, 2023

PHP is PHP. That you're running it in a way that means you can't debug it is your own problem. Unfortunately, cpanel makes many things difficult and confusing. There's really nothing I can do if you don't understand your own hosting, and it has nothing to do with PHPMailer.

// delete for website link

and put the email - [email protected] and click the submit button and scroll down the error print

@Rignesh-Sarvaliya
Copy link
Author

PHP is PHP. That you're running it in a way that means you can't debug it is your own problem. Unfortunately, cpanel makes many things difficult and confusing. There's really nothing I can do if you don't understand your own hosting, and it has nothing to do with PHPMailer.

Even if there is a problem in hosting, this error "SMTP Error: Could not connect to SMTP host. Failed to connect to server" comes?

@Synchro
Copy link
Member

Synchro commented Jan 31, 2023

Set $mail->SMTPDebug = 3; and post the output here.

@Rignesh-Sarvaliya
Copy link
Author

Set $mail->SMTPDebug = 3; and post the output here.

I have set please check

@Synchro
Copy link
Member

Synchro commented Jan 31, 2023

PHP is PHP. That you're running it in a way that means you can't debug it is your own problem. Unfortunately, cpanel makes many things difficult and confusing. There's really nothing I can do if you don't understand your own hosting, and it has nothing to do with PHPMailer.

Even if there is a problem in hosting, this error "SMTP Error: Could not connect to SMTP host. Failed to connect to server" comes?

Yes, it's a problem with your hosting, not your script or PHPMailer.

This is the important part of the error output:

Connection failed. Error #2: stream_socket_client(): unable to connect to ssl:https://smtp.gmail.com:465 (Network is unreachable)

"Network is unreachable" means that your hosting provider is blocking your outbound SMTP traffic, so PHPMailer can't work. This is what it says in the docs. It also tells you what to do in that situation, which is either ask them to stop blocking it, or find a better hosting provider.

@Rignesh-Sarvaliya
Copy link
Author

PHP is PHP. That you're running it in a way that means you can't debug it is your own problem. Unfortunately, cpanel makes many things difficult and confusing. There's really nothing I can do if you don't understand your own hosting, and it has nothing to do with PHPMailer.

Even if there is a problem in hosting, this error "SMTP Error: Could not connect to SMTP host. Failed to connect to server" comes?

Yes, it's a problem with your hosting, not your script or PHPMailer.

This is the important part of the error output:

Connection failed. Error #2: stream_socket_client(): unable to connect to ssl:https://smtp.gmail.com:465 (Network is unreachable)

"Network is unreachable" means that your hosting provider is blocking your outbound SMTP traffic, so PHPMailer can't work. This is what it says in the docs. It also tells you what to do in that situation, which is either ask them to stop blocking it, or find a better hosting provider.

ok I check thanks for the help
Thanks, man

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants