google phpmailer error SMTP Connect() failed
I'm trying to use google php mailer but i get a lot of error messages.
I've uploaded a fresh copy to test again and i get this error :
Mail error: SMTP Connect() failed.
I've enabled IMAP and POP forwarding from gmail and opened this page
https://accounts.google.com/DisplayUnlockCaptcha clicked continue and
refreshed the page to send from and it's not working i get Mail error:
SMTP Connect() failed.
Here's the code:
<?php
require_once('class.phpmailer.php');
define('GUSER', 'username@gmail.com'); // GMail username
define('GPWD', 'password'); // GMail password
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // 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;
$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;
}
}
$msg = 'Hello World';
$subj = 'test mail message';
$to = 'username@yahoo.com';
$from = 'username@gmail.com';
$name = 'any name';
if (smtpmailer($to, $from, $name, $subj, $msg)) {
echo 'Yippie, message send via Gmail';
} else {
if (!smtpmailer($to, $from, $name, $subj, $msg, false)) {
if (!empty($error)) echo $error;
} else {
echo 'Yep, the message is send (after doing some hard work)';
}
}
?>
How to solve this problem? Thanks in advance
No comments:
Post a Comment