[Solved] PHPMailer Error: SMTP ERROR: Failed to connect to server: (0)

1. Explain

After configured $mail->Username$mail->password, the error is reported as follows:

2021-09-01 12:22:37 SMTP ERROR: Failed to connect to server: (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Error

2. Solution

1. Ignore SSL authentication (this is my reason)

Add the following code after code: $mail = new PHPMailer();

// Instantiating the PHPMailer core class
$mail = new PHPMailer(); 

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

...

2. Set the port to bit 25, do not use SSL, 465 (other possible reasons)

Change

// Set up ssl encryption for login authentication
$mail->SMTPSecure = 'ssl';
// set the remote server port number for ssl connection to smtp server
$mail->Port = 465;

to

$mail->Port = 25;

Read More: