0

我试图向参加特定科目考试的学生发送大量包含密码的电子邮件。现在,我遇到错误“SMTP 错误:无法连接到 SMTP 主机。邮件程序错误() SMTP 错误:无法连接到 SMTP 主机。” 可能是什么问题?

我的代码如下:

<?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);
?>
4

3 回答 3

0
Try this...

add this code for Set secutiry

// if you're using SSL
$mail->SMTPSecure = 'ssl';
// OR use TLS
$mail->SMTPSecure = 'tls';
于 2013-08-31T11:58:16.793 回答
0

尝试使用gmail,因为我不知道 Yahoo 的 SMTP 服务器。[另外,使用 gmail 凭据(用户名、密码)]

$mail->Host = "smtp.gmail.com";
于 2013-08-31T11:59:21.600 回答
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 = "...@gmail.com";
    $mail->Password = "...";
    $mail->From = $email;
    $mail->FromName = $author;
    $mail->CharSet  = "ISO-8859-9";
    $mail->AddAddress("...@gmail.com");
    $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", "...@gmail.com", "Bla bla bla")
于 2013-08-31T12:06:17.960 回答