views:

72

answers:

5

I wrote some code to send an email from my PHP script using PHPMailer. For some reason, the scripts isn't sending the messages.


Here Is My Code:

<?php
require_once("PHPMailer/class.phpmailer.php");
$mail=new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "[email protected]";
$mail->Password = "PASSHERE";
$mail->SetFrom = "[email protected]";
$mail->AddAddress("[email protected]");
$mail->Subject = "Confirm Web Lock Registration!";
$mail->Body = "Please confirm your Web Lock Registration by clicking here!";
$mail->WordWrap = 50;

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

This Is The Error Echoed:

SMTP Error: Could not connect to SMTP host. Message was not sent.Mailer error: SMTP Error: Could not connect to SMTP host.
+1  A: 

You need to specify gmail username and password because that is the what you are using in smtp settings:

$mail->Username = "[email protected]";
$mail->Password = "yourgmailpassword";
Sarfraz
The address is the username. The company uses Google Apps for email.
Zachary Brown
+3  A: 

Your error message might be caused by the firewall settings on your server. This error message is commonly caused by a firewall blocking outgoing connections on the port.

You also should make sure that you have the openssl extension enabled.

Original Answer that you fixed:

You are sending to [email protected] which is not the address you want.

You need to remove the second .com and change it to [email protected]

Alan Geleynse
Corrected it. Still the same problem.
Zachary Brown
I added some additional problems based on the error message you gave.
Alan Geleynse
A: 

As a general tip, try turning on debugging:

$mail->SMTPDebug  = 2; // enables SMTP debug information (for testing)
                       // 1 = errors and messages
                       // 2 = messages only

From SO: Debugging PHP Mail() and/or PHPMailer

Banjer
A: 

Just as a bit of advise you might also want to try using Zend_Mail, you can use this happily without having to go the whole MVC route and it is very informative

Catharsis
+1  A: 

I'm using PHPMailer too, just tried your settings, and got the same error. Here are my settings witch work for me (I show with ->> things witch you don't have, or are different for me)

$mail->PHPMailer = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
->> $mail->SMTPSecure = "ssl";
->> $mail->Host = "smtp.gmail.com"; //not ssl://smtp.gmail.com
$mail->Port = 465; etc...

Everything else is the same, except i don't use word wrap, but i checked, it's not causing the problem.

septoned