Report this

What is the reason for this report?

Mailer error: SMTP connect() failed

Posted on June 28, 2021

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 stmp.gmail.com 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->isSMTP(true);
            $mail->Host = "smtp.gmail.com";
            $mail->SMTPAuth = true;
            $mail->Username = "my_email@gmail.com";
            $mail->Password = "my_password";
            $mail->Port = 587;
            $mail->IsHTML(true);
            $mail->CharSet = 'UTF-8';
            $mail->From = "my_email@gmail.com";
            $mail->FromName = "No Reply";
            $mail->Subject = $subject;
            $mail->Body = $body;
            $mail->AddAddress($email_to);

            if (!$mail->send()) {
			$error = 'Mail error: '.$mail->ErrorInfo; 
                echo 'send_mail_failed';
				print_r($error);
				
				
            } 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);
                $stmt_gen_pass_update->execute();
                $stmt_gen_pass_update->closeCursor();

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


This textbox defaults to using Markdown to format your 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 :

https://www.digitalocean.com/support/

Please do let me know how it goes!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.