Email using PHPmailer to gmail giving SMTP Error?

I am trying to send email using PHPmailer with google SMTP server, it is giving an SMTP error(SMTP Error:could not connect to SMTP host),I am showing the code below:-

<?
if(isset($_POST['email']))
{
	$first_name = $_POST['name']; // required
	$email_from = $_POST['email']; // required
	$phone = $_POST['phone']; // not required
	$comments = $_POST['comments']; // required
														
														
	$email_message .= "First Name: ".$first_name."\
\
";
	$email_message .= "Email: ".$email_from."\
\
";
	$email_message .= "Telephone: ".$phone."\
\
";
	$email_message .= "Comments: ".$comments."\
";
														
	require_once('PHPMailer_v5.1/class.phpmailer.php');
	$mail = new PHPMailer();
	$mail->IsSMTP();  // telling the class to use SMTP
	$mail->SMTPAuth = true; // enable SMTP authentication
	$mail->Username = "username"; // SMTP username
	$mail->Password = "password"; // SMTP password
														
	$mail->SMTPSecure = "ssl"; // sets the prefix to the server
	$mail->Host     = "smtp.gmail.com"; // sets GMail as the SMTP  server
	$mail->Port = 465;  // set the SMTP port for the GMAIL server
														 
	$mail->From = $email_from;
	$mail->FromName = $first_name;
	$mail->SetFrom($email_from, $first_name);
	$mail->AddReplyTo($email_from, $first_name);
	$mail->AddAddress("sureshbaburavilla@gmail.com",$first_name);
	$mail->AddCC("sridhar.425@gmail.com",$first_name);
														    $mail->AddBCC("srinivasvar@gmail.com",$first_name);													 
	$mail->Subject  = "Sree Vizag Marketing-Tiles Enquiry";
	$mail->Body     = $email_message;
	$mail->WordWrap = 200;
														 
	if(!$mail->Send()) 
	    {
	       echo "<script>alert('Appears to be Server Problem! Please contact again!');window.location='contact.html';</script>";
	    }
	else
	    {
		echo "<script>alert('Email sent,get back to you soon!');window.location='index.html';</script>";
	    }
				
}
												
?>

I have also used:-

<?
$mail->SMTPSecure = "tls"; // sets the prefix to the server
$mail->Port = 587;  // set the SMTP port for the GMAIL server
?>

It is also giving the same SMTP error

Any solution to this will be gladly appreciated.

dl() is not supported in multithreaded Web servers, that’s why I use task scheduler to run my php file:


<?php
dl("php_openssl.dll");

require("c:\\apache\\includes\\class.phpmailer.php");

$mail = new PHPMailer();

$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "xxx";
$mail->Password = "";
$mail->Port = 465;


$from = "xxx";
$to = "xxx";
$name = "xxx";

$mail->From = $from;
$mail->FromName = "webmaster";
$mail->AddAddress($to, $name);
$mail->AddReplyTo($from, "webmaster");

$mail->WordWrap = 50;

$mail->IsHTML(true);
$mail->Subject = "Sending Email From Php Using Gmail";
$mail->Body = "This Email Send through phpmailer, This is the HTML BODY";
$mail->AltBody = "This is the body when user views in plain text format";

if (!$mail->Send())
{
  echo "Message was not sent<br/ >";
  echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
  echo "Message has been sent";
}