我試圖發送大量的電子郵件包含密碼給參加考試的學生在一個特定的subject.Now,我有一個錯誤「SMTP錯誤:無法連接到SMTP主機郵件錯誤()SMTP錯誤:無法連接到SMTP主機。「可能是什麼問題?發送郵件與PHPMailer
我的代碼如下:
<?php
//error_reporting(E_ALL);
error_reporting(E_STRICT);
//date_default_timezone_set('America/Toronto');
require_once('PHPMailer-phpmailer-5.2.0/class.phpmailer.php');
include("PHPMailer-phpmailer-5.2.0/class.smtp.php"); //optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "localhost";
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent
$mail->Host = "mail.yahoo.com"; // sets the SMTP server*/
$mail->Port = 26;
$mail->Username = "***********@yahoo.com"; // SMTP account username
$mail->Password = "****************"; // SMTP account password
$mail->From = "*************@yahoo.com";
$mail->FromName = "Exam System";
//$mail->IsHTML(true);
while ($row_email = mysql_fetch_array ($email)) {
$mail->Subject = "Subject: ".$row_email['subject_description']."";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->Body = "This is your password for ".$row_email['exam_title']." : ".$row_email['pass_password']."";
$mail->AddAddress($row_email['stud_email']);
if(!$mail->Send()) {
echo "Mailer Error (" . str_replace("@", "@", $row_email['stud_email']) . ') ' . $mail->ErrorInfo . '<br />';
} else {
echo "Message sent to :" . $row_email['stud_email'] . ' (' . str_replace("@", "@", $row_email['stud_email']) . ')<br />';
}
// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
$mail->ClearAttachments();
}
mysql_free_result($email);
?>
端口26看起來是錯誤的。 SMTP通常在端口25上發送。請嘗試刪除此行,以使庫默認爲正確的端口? – halfer
設置安全性(TLS/SSL)並嘗試將端口更改爲465. –
仍然具有相同錯誤。我已將端口更改爲465並添加了以下行:$ mail-> SMTPSecure ='tls';即使我的電子郵件帳戶在雅虎上,我也可以使用Gmail嗎? –