Sunday 4 December 2016

php - Phpmailer using smtp with Gmail not working - connection timing out

I've looked into the following links:



phpmailer send gmail smtp timeout




send email using Gmail SMTP server through PHP Mailer



http://uly.me/phpmailer-and-gmail-smtp/



...and tried to implement for myself a combination of those however...most of the time it sends this message...




Message could not be sent.




Mailer Error: SMTP connect() failed.




However there was one time where it sent this when I experimented between "tls" and "ssl"...




SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed.
Message could not be sent.



Mailer Error: SMTP connect() failed.





My code is attached...did I somehow miss something? I asked the web hosting service if they're blocking and gave them a template of my code - they said the server allows connections to Gmail's SMTP.



    require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail -> IsSMTP();
$mail -> SMTPDebug = 2;
$mail -> SMTPAuth = 'true';
$mail -> SMTPSecure = 'tls';

$mail -> SMTPKeepAlive = true;
$mail -> Host = 'smtp.gmail.com';
$mail -> Port = 587;
$mail -> IsHTML(true);

$mail -> Username = "myemail@gmail.com";
$mail -> Password = "mypassword";
$mail -> SingleTo = true;

$to = xxx;

$from = xxx;
$fromname = xxx;
$subject = xxx;
$message = xxx
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";

$mail -> From = $from;
$mail -> FromName = $fromname;

$mail -> AddAddress($to);

$mail -> Subject = $subject;
$mail -> Body = $message;

if(!$mail -> Send()){
echo "Message could not be sent.

";
echo "Mailer Error: " . $mail-> ErrorInfo;
exit;
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...