8
<?php

include("class.phpmailer.php");
include("class.smtp.php");

$mail = new PHPMailer();

$mail->IsSMTP();  // telling the class to use SMTP
$mail->Mailer = "smtp";
$mail->SMTPDebug = 2;
$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server
$mail->Port = 587; // set the port to use
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "tls";

$mail->Username = "123@gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password 

$mail->From = "123@gmail.com";
$mail->FromName = "Webmaster";

$mail->AddAddress("asd@hotmail.com");
$mail->AddReplyTo("123@gmail.com", "Webmaster");
$mail->IsHTML(true);

$mail->Subject  = "First PHPMailer Message";
$mail->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;

if(!$mail->Send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}
?>

It returns an error

2016-04-01 08:41:43 SMTP ERROR: Failed to connect to server: (0) 
2016-04-01 08:41:43 SMTP connect() failed. 

https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message was not sent.Mailer error: SMTP connect() failed. 

https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

I am hosting this php on my xampp local server. The extension=php_openssl.dll on php.ini is already uncommended.

Gavin
  • 2,784
  • 6
  • 41
  • 78
  • You are using an old version of PHPMailer and you have based your code on an obsolete example. [Get the latest version](https://github.com/PHPMailer/PHPMailer) and use [the gmail example provided](https://github.com/PHPMailer/PHPMailer/tree/master/examples). This is **the very first thing** the troubleshooting guide (linked from your error message) says to check, but you evidently didn't read that. – Synchro Apr 01 '16 at 09:28
  • 3
    Possible duplicate of [send email using Gmail SMTP server through PHP Mailer](https://stackoverflow.com/questions/16048347/send-email-using-gmail-smtp-server-through-php-mailer) – Félix Adriyel Gagnon-Grenier Dec 18 '17 at 20:47
  • 1
    In case somebody lands here, this is what happened to me: wrong case (`port` instead of `Port`). Careful! – Wtower Jan 28 '19 at 13:14

1 Answers1

5

Your configuration might be wrong. I believe that if you change your host into smtp.gmail.com it might solve your problem.

I noticed that you set security tls but you want to connect with ssl as well.

Change $mail->Host = "ssl://smtp.gmail.com"; into $mail->Host = "smtp.gmail.com"; and security to ssl.

From this answer:

$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "email@gmail.com";
$mail->Password = "password";
$mail->SetFrom("example@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email@gmail.com");

 if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
 } else {
    echo "Message has been sent";
 }
  • This solved the connection problem. But I had a little further issue, google mail seems to be blocking access. http://stackoverflow.com/questions/20337040/gmail-smtp-debug-error-please-log-in-via-your-web-browser This solved the security issue – Gavin Apr 01 '16 at 09:21
  • Please don't post code based on obsolete examples. Use [the examples provided with PHPMailer](https://github.com/PHPMailer/PHPMailer/tree/master/examples) as they are kept up to date. – Synchro Apr 01 '16 at 09:25