Mailer error: SMTP connect() failed

I’m stuck at an error sending mail to the user. I am using VPS Centos 7, and running php web using phpmailer to proceed with sending mail as below structure. It works perfectly fine when running on xampp localhost, but when I run on Centos I get Mailer error: SMTP connect() failed . I tried everything but still can’t fix it, I tried commenting the line $mail->isSMTP(true); it works, but no email is sent to the client. I also tried checking port 587, 25, and 465 to see if it listens via telnet : telnet 587(25,465) and the result is 220. I also installed phpmailer on centos, but not sure why it won’t connect to stmp server. Hope everyone can help me with this problem.

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

require_once "../vendor/autoload.php";

            $mail = new PHPMailer;
            $mail->Host = "";
            $mail->SMTPAuth = true;
            $mail->Username = "";
            $mail->Password = "my_password";
            $mail->Port = 587;
            $mail->CharSet = 'UTF-8';
            $mail->From = "";
            $mail->FromName = "No Reply";
            $mail->Subject = $subject;
            $mail->Body = $body;

            if (!$mail->send()) {
			$error = 'Mail error: '.$mail->ErrorInfo; 
                echo 'send_mail_failed';
            } else {
                $update_password_byEmail = "UPDATE account SET password=:gen_password WHERE name=:currentUser AND email=:currentEmail;";
                $stmt_gen_pass_update = $sqlWeb->prepare($update_password_byEmail);
                $stmt_gen_pass_update->bindValue(':gen_password', md5($password_generate));
                $stmt_gen_pass_update->bindValue(':currentUser', $usernameOfEmail);
                $stmt_gen_pass_update->bindValue(':currentEmail', $target_email);

                echo 'send_mail_successful';
        } else {
		$error = 'Mail error: '.$mail->ErrorInfo; 
            echo 'send_mail_error';
			//echo $error;

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi @vin2164382,

In some cases, DigitalOcean rate limit new accounts and block ports 25,465 and 587 by default even if you allow your ports in your firewall.

My suggestion if you have allowed outgoing connections on ports 465 and 587 on your firewall and it still doesn’t work is to contact DigitalOcean’s support and ask them if your SMTP ports are blocked.

You can contact them at :

Please do let me know how it goes!