0

I am trying to send a mail via phpMailer but I got below error

2019-03-28 10:54:00 SMTP ERROR: Failed to connect to server: (0) 2019-03-28 10:54:00 SMTP connect() failed. Mailer Error: SMTP connect() failed.

I referred below links

https://stackoverflow.com/questions/43815571/mailer-error-smtp-connect-failed
https://stackoverflow.com/questions/21088062/phpmailer-smtp-connection-error
https://stackoverflow.com/questions/43999573/phpmailer-smtp-connect-error
https://stackoverflow.com/questions/36735979/mailer-error-smtp-connect-failed
https://stackoverflow.com/questions/30476024/smtp-connect-failed-phpmailer

but after implementing the solution as mention in the above links, getting the same error.

I tried with PHP mail() function for the same credential, it's working but I wanted to send a mail via SMTP.

here is my code

require('mail/class.phpmailer.php');
require('mail/PHPMailerAutoload.php');
$mail = new PHPMailer;
$mail->SMTPDebug = 2;
$mail->isSMTP(); 
$mail->Mailer = "smtp";                                            
$mail->Host       = 'mail.example.com';  
$mail->SMTPAuth   = true;                                  
$mail->Username   = 'From@example.com';                     
$mail->Password   = 'password';                             
$mail->SMTPSecure = 'ssl'; 
$mail->Port       = 465;                                    
$mail->setFrom('From@example.com', 'from');
$mail->addAddress('To@example.com', 'To');
$mail->isHTML(true);                               
$mail->Subject = 'subject';
$mail->Body    = 'This is message';
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
kushal
  • 1
  • Despite reading all those links, you're still using an old version of PHPMailer (incorrectly), have code based on an obsolete example, and have not read [the troubleshooting guide](https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting). Try a bit harder. – Synchro Mar 28 '19 at 11:57
  • hello @Synchro, I am already using a new version of phpMailer, where did you find a that I am using an older version of phpMailer. I downloaded the phpMailer form below link https://github.com/PHPMailer/PHPMailer. I included only the src folder from your phpMailer file in my project. – kushal Mar 29 '19 at 05:24
  • The very first line: the file `class.phpmailer.php` was removed from main PHPMailer releases over a year ago. – Synchro Mar 29 '19 at 06:00
  • I apologize for my mistake. I updated the first two line as I mention below `require ('PHPMailer/src/Exception.php'); require ('PHPMailer/src/PHPMailer.php'); require ('PHPMailer/src/SMTP.php');` also, I added these two lines top of the code `use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception;` and make ` $mail = new PHPMailer(true);` but still getting a same error. – kushal Mar 29 '19 at 07:48
  • So what happened when you went through the tests in the troubleshooting guide? – Synchro Mar 29 '19 at 08:39

0 Answers0