簡體   English   中英

郵件不使用SMTP通過SSL發送PHPMailer

[英]Mail not sending with PHPMailer over SSL using SMTP

我試圖使用PHPMailer通過SMTP發送電子郵件,但到目前為止沒有運氣。 我已經閱讀了許多SO問題,PHPMailer教程和論壇帖子,但仍然無法使其工作。 我將記錄盡可能多的失敗嘗試,因為我記得節省時間,但首先是我使用的代碼:

<?php
    session_start();
    error_reporting(E_ALL);
    ini_set('display_errors','On');

    require('includes/class.phpmailer.php');
    include('includes/class.smtp.php');
    $mail = new PHPMailer(); 

    $name = $_POST["name"];
    $guests = $_POST["guests"];
    $time = $_POST["time"];

    $message = "<h1>".$name." has booked a table for ".$guests." at ".$time."</h1>";

    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->Host       = "ssl://smtp.gmail.com"; // SMTP server
    $mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
    $mail->Username   = "myEmail@gmail.com"; // SMTP account username
    $mail->Password   = "myPassword";        // SMTP account password
    $mail->SetFrom('myEmail@gmail.com', 'James Cushing');
    $mail->AddReplyTo("myEmail@gmail.com","James Cushing");
    $mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";
    $mail->MsgHTML($message)
    $address = "myOtherEmail@me.com";
    $mail->AddAddress($address, "James Cushing");

    if(!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }
?>

首先,當我運行此代碼時,我得到兩個不同的錯誤。 在我的本地服務器上,我收到錯誤:
SMTP - >錯誤:無法連接到服務器:操作超時(60)
以下發件人地址失敗:myEmail@gmail.com:調用Mail()而未連接
郵件程序錯誤:以下發件人地址失敗:myEmail@gmail.com:調用Mail()而未連接

我在Web服務器上運行相同的代碼時遇到同樣的錯誤,但第一行是:
SMTP - >錯誤:無法連接到服務器:網絡無法訪問(101)

顯然值得指出的是,我沒有使用文字“myEmail@gmail.com”,但我已經用自己的電子郵件替換了這篇文章。

我嘗試過的事情
- 使用iCloud SMTP服務器
- 使用不同的端口
- 在我的php.ini文件中啟用OpenSSL擴展
- 從各種PHPMailer示例中復制代碼
- 使用Google的“DisplayUnlockCaptcha”系統啟用連接
- 發送到不同地址和從不同地址發送 - 從Username屬性中刪除“@ gmail.com” - 其他一些我不記得的事情

這已經讓我瘋了大約一天,所以如果有人能解決它,他們將成為英雄。

謝謝

$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "myemail@gmail.com";
$mail->Password = "**********";
$mail->Port = "465";

這是一個有效的配置。

嘗試替換你擁有的東西

不要在端口465上使用SSL,它自1998年以來一直被棄用,僅供未獲得備忘錄的Microsoft產品使用; 改為在端口587上使用TLS:因此,下面的代碼應該可以很好地為您服務。

mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "smtp.gmail.com"; // SMTP server

$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "tls";                 // 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 

首先,為Google使用以下設置:

$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls"; //edited from tsl
$mail->Username = "myEmail";
$mail->Password = "myPassword";
$mail->Port = "587";

但是,你設置了什么防火牆?

如果您要過濾掉TCP端口465/995(可能是587),則需要配置一些例外或將它們從規則列表中刪除。

https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

每當我的客戶端機器改變網絡連接(例如,家庭與辦公室網絡)並且以某種方式重新啟動網絡服務(或重新啟動機器)時,我就會遇到類似的SMTP故障,這對我來說是一個問題。 不確定這是否適用於您的情況,但以防萬一。

sudo /etc/init.d/networking restart   # for ubuntu

首先,谷歌創建了“使用不太安全的帳戶方法”功能:

https://myaccount.google.com/security

然后創建了另一個權限:

https://accounts.google.com/b/0/DisplayUnlockCaptcha

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM