-1

I have been searched many hours to solve my problem and i failed.

This is my simple code use PHPMailer to send email with Gmail in xampp and get this error each time:

Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

and this is my php code :

<?php
 require_once('PHPMailer/PHPMailerAutoload.php');

 $mail = new PHPMailer();
 $mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
 $mail->HOST = 'smtp.gmail.com';
 $mail->PORT = '465';
 $mail->isHTML();
 $mail->Username = '##myemail##@gmail.com';
 $mail->Password = '##mypass##';
 $mail->SetFrom('no-reply@howcode.org');
 $mail->Subject = 'Hello World';
 $mail->Body = 'A test email!';
 $mail->AddAddress('hamidreza.mohammadian@yahoo.com');


 if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
 } else {
    echo 'Message has been sent';
 }

?>

please help me.

ihamiiid
  • 11
  • 1
  • 5
  • 1
    Did you check the wiki? https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#smtp-error-could-not-connect-to-smtp-host – modsfabio Apr 11 '17 at 11:27
  • You most probably have authentication error but you should set `$mail->SMTPDebug = 2` and let us know what the error is. – Linek Apr 11 '17 at 11:34
  • 1
    Possible duplicate of [PHPMailer, SMTP connect() failed error with Gmail](http://stackoverflow.com/questions/25924651/phpmailer-smtp-connect-failed-error-with-gmail) – Synchro Apr 11 '17 at 12:43
  • This question is identical to many others. A complete guide to troubleshooting this is linked from that error message, but you ignored that. You didn't [search very hard](http://stackoverflow.com/search?q=PHPMailer+SMTP+connect%28%29+failed). – Synchro Apr 11 '17 at 12:45
  • that's the message 2017-04-11 12:45:45 SMTP ERROR: Failed to connect to server: No connection could be made because the target machine actively refused it. (10061) 2017-04-11 12:45:45 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting – ihamiiid Apr 11 '17 at 12:46

1 Answers1

0

Add this $mail->SMTPDebug = 3; $mail->isHTML(true); to output debug. But make sure you have activated connection by insecure apps using Gmail. Look a this link https://support.google.com/a/answer/6260879?hl=en

Ulrich Dohou
  • 1,509
  • 14
  • 25