Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SMTP connect() failed PHPmailer - PHP

Tags:

php

email

I am new to PHP. I was trying to send myself a sample e-mail through PHPmailer. I am using gmail's smtp server. I am trying to send a sample mail from my gmail account to my yahoo account. But I am getting the error : Mailer Error: SMTP connect() failed.
Here is the code :

<?php  require "class.phpmailer.php"; $mail = new PHPMailer();  $mail->IsSMTP();                              // send via SMTP $mail->Host = "ssl://smtp.gmail.com"; $mail->SMTPAuth = true;                       // turn on SMTP authentication $mail->Username = "[email protected]";        // SMTP username $mail->Password = "mypassword";               // SMTP password $webmaster_email = "[email protected]";       //Reply to this email ID $email="[email protected]";                // Recipients email ID $name="My Name";                              // Recipient's name $mail->From = $webmaster_email; $mail->Port = 465; $mail->FromName = "My Name"; $mail->AddAddress($email,$name); $mail->AddReplyTo($webmaster_email,"My Name"); $mail->WordWrap = 50;                         // set word wrap $mail->IsHTML(true);                          // send as HTML $mail->Subject = "subject"; $mail->Body = "Hi, This is the HTML BODY ";                      //HTML Body  $mail->AltBody = "This is the body when user views in plain text format"; //Text Body   if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message has been sent"; } ?> 

I am using WAMP server on a Windows 7 64-bit machine. What could be the prob?
Please help me solve this. Thanks!

like image 501
Chinmay Dabke Avatar asked Apr 08 '14 04:04

Chinmay Dabke


People also ask

How can I get SMTP error in php?

There are many popular cases for the failure of SMTP connection in PHPMailer and lack of SSL is one of that too. There might be a case, that the Open SSL extension is not enabled in your php. ini which is creating the connection problem. So, once you enable the extension=php_openssl.

Is PHPMailer SMTP?

PHPMailer can use a non-local mail server (SMTP) if you have authentication. Further advantages include: It can print various kinds of error messages in more than 40 languages when it fails to send an email. It has integrated SMTP protocol support and authentication over SSL and TLS.


2 Answers

The solution of this problem is really very simple. actually Google start using a new authorization mechanism for its User.. you might have seen another line in debug console prompting you to log into your account using any browser.! this is because of new XOAUTH2 authentication mechanism which google start using since 2014. remember.. do not use the ssl over port 465, instead go for tls over 587. this is just because of XOAUTH2 authentication mechanism. if you use ssl over 465, your request will be bounced back.

what you really need to do is .. log into your google account and open up following address https://www.google.com/settings/security/lesssecureapps and check turn on . you have to do this for letting you to connect with the google SMTP because according to new authentication mechanism google bounce back all the requests from all those applications which does not follow any standard encryption technique.. after checking turn on.. you are good to go.. here is the code which worked fine for me..

require_once 'C:\xampp\htdocs\email\vendor\autoload.php';  define ('GUSER','[email protected]'); define ('GPWD','your password');   // make a separate file and include this file in that. call this function in that file.  function smtpmailer($to, $from, $from_name, $subject, $body) {      global $error;     $mail = new PHPMailer();  // create a new object     $mail->IsSMTP(); // enable SMTP     $mail->SMTPDebug = 2;  // debugging: 1 = errors and messages, 2 = messages only     $mail->SMTPAuth = true;  // authentication enabled     $mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail     $mail->SMTPAutoTLS = false;     $mail->Host = 'smtp.gmail.com';     $mail->Port = 587;      $mail->Username = GUSER;       $mail->Password = GPWD;                $mail->SetFrom($from, $from_name);     $mail->Subject = $subject;     $mail->Body = $body;     $mail->AddAddress($to);     if(!$mail->Send()) {         $error = 'Mail error: '.$mail->ErrorInfo;          return false;     } else {         $error = 'Message sent!';         return true;     } } 
like image 124
Rizwan Akhwandzada Avatar answered Sep 23 '22 00:09

Rizwan Akhwandzada


You need to add the Host parameter

$mail->Host = "ssl://smtp.gmail.com";  

Also, check if you have open_ssl enabled.

<?php echo !extension_loaded('openssl')?"Not Available":"Available"; 
like image 35
Shankar Narayana Damodaran Avatar answered Sep 23 '22 00:09

Shankar Narayana Damodaran