2013-08-31 166 views
0

我試圖發送大量的電子郵件包含密碼給參加考試的學生在一個特定的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("@", "&#64;", $row_email['stud_email']) . ') ' . $mail->ErrorInfo . '<br />'; 
    } else { 
    echo "Message sent to :" . $row_email['stud_email'] . ' (' . str_replace("@", "&#64;", $row_email['stud_email']) . ')<br />'; 
    } 
    // Clear all addresses and attachments for next loop 
    $mail->ClearAddresses(); 
    $mail->ClearAttachments(); 
} 


mysql_free_result($email); 
?> 
+0

端口26看起來是錯誤的。 SMTP通常在端口25上發送。請嘗試刪除此行,以使庫默認爲正確的端口? – halfer

+0

設置安全性(TLS/SSL)並嘗試將端口更改爲465. –

+0

仍然具有相同錯誤。我已將端口更改爲465並添加了以下行:$ mail-> SMTPSecure ='tls';即使我的電子郵件帳戶在雅虎上,我也可以使用Gmail嗎? –

回答

0
Try this... 

add this code for Set secutiry 

// if you're using SSL 
$mail->SMTPSecure = 'ssl'; 
// OR use TLS 
$mail->SMTPSecure = 'tls'; 
0

嘗試使用Gmail的,因爲我不知道雅虎的SMTP服務器。 [此外,使用Gmail的憑據(用戶名,密碼)]

$mail->Host = "smtp.gmail.com"; 
0
function SendMailWithGmailSMTP($author, $email, $text) 
{ 
    $mail = new PHPMailer(); 
    $mail->IsSMTP(); 
    $mail->Host  = "localhost"; // Sets the SMTP hosts 
    $mail->Port  = 587; // Sets the default SMTP server port. 
    $mail->SMTPAuth = false; // Sets SMTP authentication. Utilizes the Username and Password variables. 
    $mail->Username = "[email protected]"; 
    $mail->Password = "..."; 
    $mail->From = $email; 
    $mail->FromName = $author; 
    $mail->CharSet = "ISO-8859-9"; 
    $mail->AddAddress("[email protected]"); 
    $mail->Subject = "Hi. This is a Subject!"; 
    $mail->IsHTML(true); 
    $mail->Body = $text; 
    if($mail->Send()) return true; else echo '<script language="javascript">alert("'.$mail->ErrorInfo.'");</script>'; 
} 

寫此功能發送郵件,並調用這個函數是這樣的:

SendMailWithGmailSMTP("Aziz Yılmaz", "[email protected]", "Bla bla bla") 
+0

您的代碼安全嗎?如果我被禁止?沒有使用smtpauth它是安全的嗎? –