I am trying to send SMTP email using my gmail account details through PHP but every time I try got an error. I am writing my code so you can see what is the problem.
PHP:
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//Enable SMTP debugging.
$mail->SMTPDebug = 3;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = "smtp.gmail.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = "username";
$mail->Password = "password";
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";
//Set TCP port to connect to
$mail->Port = 587;
$mail->From = "sender email";
$mail->FromName = "sender name";
$mail->addAddress("receiver email");
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
?>
and the error I am facing:
2016-08-12 00:22:02 Connection: opening to smtp.gmail.com:587,
timeout=300, options=array ( ) 2016-08-12 00:22:16 SMTP ERROR: Failed to
connect to server: Network is unreachable (101) 2016-08-12 00:22:16 SMTP
connect() failed. Mailer Error: SMTP connect() failed.
any help would be appreciated. Thanks