我試圖通過PHPMailer的從我的Gmail發送郵件,但我得到了以下錯誤:PHPMailer的 - 無法連接到SMTP
SMTP Error: Could not connect to SMTP host.
我下載的PHPMailer類從:https://github.com/Synchro/PHPMailer。
我試了超過三十個小時,所以我試過了端口25,465,587的大部分組合。我甚至嘗試過從我的SMTP用戶名中刪除@ gmail.com這樣的奇怪東西,所以請幫助我。
<?php
/**
* Simple example script using PHPMailer with exceptions enabled
* @package phpmailer
* @version $Id$
*/
require '../class.phpmailer.php';
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$body = file_get_contents('contents.html');
$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = 'ssl';
$mail->Port = 465; // set the SMTP server port
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Username = "[email protected]"; // SMTP server username
$mail->Password = "mypass"; // SMTP server password
// $mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("[email protected]in.com","First Last");
$mail->From = "[email protected]";
$mail->FromName = "First Last";
$to = "[email protected]";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>
查看此文章:https://stackoverflow.com/questions/37079061/phpmailer-saying-could-not-connect-to-smtp-host –