SMTP Error: Could not connect to SMTP host.
can some one check if i doing this right?

 $mail = new PHPMailer();
                    $mail->IsSMTP(); 
                    $mail->Mailer = "smtp";
                    $mail->Host = " smtp.gmail.com";
                    $mail->SMTPAuth = true;
                    $mail->SMTPSecure = "ssl";  //ssl or tls

                    //$mail->Host = "ssl://smtp.gmail.com";                         
                    $mail->Port = 465;              //set smtp port for the gmail
                    $mail->Username = "test1@gmail.com";    //yourname@yourdomain
                    $mail->Password = "asdfasdf";     //password

                    //to
                    $mail->AddAddress("test2@hotmail.com"); //to:   
                    //subject
                    $mail->Subject = "tttt";
                    //html body
                    $mail->IsHTML(true);
                    $mail->Body = "HEllow ikhlas";
                    //from
                    $mail->From = "test1@gmail.com";
                    $mail->FromName = "test";
                    $mail->wordWrap = 50;
                    //send
                    if(!$mail->send())
                    {
                        $forgot_error .= "Mail error ".$mail->ErrorInfo;
                    }
                    else
                    {
                        $forgot_error .= "E-mail has be sent to <b>$email_f</b>";
                    }

do i need to change gmail setting??

Yes. Search this forum. This has been discussed a lot of times. IIRC the ssl port is 995, but not 100% sure.

i tried searching for it but all i keep on finding is phpmailer functions and example of how to use them.
do u happen to remember what are the gmail setting?

on my gmail i did change IMAP to enabled

You have blank space in host name.

Also add $mail->SMTPDebug = 2; in your code for debugging.

$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
commented: ty +0

Code i have tried:

<html>
<head>
<title>PHPMailer - SMTP (Gmail) basic test</title>
</head>
<body>
<?php
error_reporting(E_STRICT);

date_default_timezone_set('America/Toronto');
require("phpmailer/class.phpmailer.php");

$mail = new PHPMailer();

$body = "this is <strong>testing</strong> mail ". date('Y-m-d H:i:s');

$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "****@***.com";  // GMAIL username
$mail->Password   = "*****";            // GMAIL password

$mail->SetFrom('name@yourdomain.com', 'First Last');
$mail->AddReplyTo("name@yourdomain.com","First Last");
$mail->Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);

$address = "youraddress@test.com"; // add your address here 
$mail->AddAddress($address, "Gmail Test");

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}
?>
</body>
</html>
commented: :) +0
SMTP -> ERROR: Failed to connect to server: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? (32) 
SMTP Error: Could not connect to SMTP host.

so wired i have same as you still 




    $mail = new PHPMailer();
                        $mail->IsSMTP(); 
                        $mail->SMTPDebug  = 2; 
                        $mail->Mailer = "smtp";
                        $mail->Host = "smtp.gmail.com";
                        $mail->SMTPAuth = true;
                        $mail->SMTPSecure = "ssl";  //ssl or tls                        
                        $mail->Port = 465;              //set smtp port for the gmail
                        $mail->Username = "sender@gmail.com";    //yourname@yourdomain
                        $mail->Password = "*******";     //password

                        //to
                        $mail->AddAddress("reserver@hotmail.com"); //to:    
                        //subject
                        $mail->Subject = "test1";
                        //html body
                        $mail->IsHTML(true);
                        $mail->Body = "HEllow ";
                        //from
                        $mail->From = "sender@gmail.com";
                        $mail->FromName = "test";
                        $mail->wordWrap = 50;
                        //send
                        if(!$mail->send())
                        {
                        }
                        else
                        {
                        }

You need to enable OpenSSL extension.
Edit your php.ini file and remove comma in front of openssl.

extension=php_openssl.dll

commented: ty +0

where do i put php.ini file and do i include in my code?

and what is extension=php_openssl.dll

sorry for asking to many question but this is my first time doing this

Add below code in test.php file and run it in browser, you will see all PHP configuration.

<?php
phpinfo();
phpinfo(INFO_MODULES);
?>

Now look into Loaded Configuration File , it will show php.ini file path.

commented: ty +0

i found php.ini file but there is no openssl.dll

I forget to tell you, if you do any changes in php.ini file, you need to restart your server to see changes.

commented: so happy this works! +0

there is no extension=php_openssl.dll in my php.ini file.
xampp->php->php.ini

It should be. I think just before few mins you have post all extensions from php.ini and it was there,
Even when you check php configuration from browser (from test.php and code which i have posted above), you will see box for "openssl".

commented: ty +0

in my php.ini there is no openssl. so i download another one from online which had openssl.

so i del my old php.ini and add the new php.ini.
but then i cant start my apache.
only way i can start my apache if i add my old php.ini file which dont have openssl.

woooow it works now thats alot vibhaj.

When you up vote any post, add some comments, so i can get reputation points :)

commented: thanks alot for your help +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.