我正在尝试通过PHP使用我的Gmail帐户详细信息发送SMTP电子邮件,但每次尝试都会出错。我正在编写代码,以便您可以看到问题所在。
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";
}
?>
和我面临的错误:
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.
任何帮助将不胜感激。 谢谢