使用PHPMailer发送邮件

时间:2013-08-31 11:50:57

标签: php phpmailer

我试图将大量包含密码的电子邮件发送给在特定主题上参加考试的学生。现在,我有一个错误“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);
?>

3 个答案:

答案 0 :(得分:0)

Try this...

add this code for Set secutiry

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

答案 1 :(得分:0)

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

$mail->Host = "smtp.gmail.com";

答案 2 :(得分: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")