[英]getting SMTP ERROR: Failed to connect to server
我正在使用phpmailer發送郵件。 但是,當我提交應用程序時,這就是我得到的SMTP->錯誤:無法連接到服務器:連接被拒絕(111)
<?php
require 'classes/class.phpmailer.php';
require 'classes/class.smtp.php';
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
// $mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "test2gmail.com"; // GMAIL username
$mail->Password = "test123"; // GMAIL password
$mail->AddAddress('test@gail.com', 'name');
$mail->SetFrom('test@gmail.com', 'test');
$mail->AddReplyTo('test@gmail.com', 'testing');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('contents.html'));
// $mail->AddAttachment('images/phpmailer.gif'); // attachment
// $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
您的服務器不允許您使用該端口,或者gmail服務器不允許您的服務器進行連接。
在本地嘗試相同的腳本,它應該可以工作。
變化:
$mail = new PHPMailer;
try {
// remove comment and add your mailing server
$mail->Host = "mail.yourdomain.com";
// if not working then change "ssl" to "tls"
$mail->SMTPSecure = "tls";
// change port 465 to 25 in case if not work
$mail->Port = 465;
......
嘗試這些更改,它可以工作。 祝一切順利.. :)
1-將端口更改為587
$Mail->Port= 587;
2-更改您的帳戶安全設置
https://www.google.com/settings/security/lesssecureapps
並嘗試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.