使用PHP邮件程序发送2封电子邮件失败

时间:2012-11-26 08:39:02

标签: php email phpmailer

我对这个感到很困惑。

//SMTP servers details
$mail->IsSMTP(); 
$mail->Host = "mail.hostserver.com";  
$mail->SMTPAuth = false;     
$mail->Username = $myEmail;  // SMTP usr
$mail->Password = "****";    // SMTP pass
$mail->SMTPKeepAlive = true;   
$mail->From = $patrickEmail; 
$mail->FromName = "***";    
$mail->AddAddress($email, $firstName . " " . $lastName); 
$mail->WordWrap = 50;                                 
$mail->IsHTML(true);                                  
$mail->Subject = $client_subject;
$mail->Body    = $client_msg;

if($mail->Send())
{

$mail->ClearAllRecipients(); 
$mail->ClearReplyTos();
$mail->ClearCustomHeaders();
...
$mail->From = "DO_NOT_REPLY@..."; 
$mail->FromName = "****";    
$mail->AddAddress($ToEmail1, "***"); //To: (recipients).
$mail->AddAddress($ToEmail2, "***"); //To: (recipients).
$mail->WordWrap = 50;            
$mail->IsHTML(true);             
$mail->Subject = $notification_subject;
$mail->Body    = $notification_msg;
if($mail->Send())
{
...

第一封电子邮件发送正常。第二个不是。这种行为可能是什么原因?我错过了某种重置吗?


更新:使用不同的邮件服务器似乎工作,显然它是该特定邮件服务器的设置导致问题。知道那可能是什么吗?

8 个答案:

答案 0 :(得分:8)

某些提供商对可在特定时间跨度内发送的邮件数量施加限制。要确定您的问题是否取决于提供商的“速率限制”,您应该尝试在第一次发送后添加暂停。例如:

if ($mail->Send()) {
    sleep(10);  // Seconds
    ...
    if ($mail->Send()) {
        ...
    }
}

然后,通过逐步降低睡眠时间,您应该能够确定哪个是速率限制。

答案 1 :(得分:3)

恕我直言,您需要为每封已发送的电子邮件创建新的PHPMailer对象。如果您想共享一些常见的设置,请使用以下内容:

$mail = new PHPMailer();
/* Configure common settings */

while ($row = mysql_fetch_array ($result)) {
    $mail2 = clone $mail;
    $mail2->MsgHTML("Dear ".$row["fname"].",<br>".$cbody);
    $mail2->AddAddress($row["email"], $row["fname"]);
    $mail2->send();
}

答案 2 :(得分:3)

试试这个: 正如@Felipe Alameda A提到Remove $mail->SMTPKeepAlive = true;

// for every mail
if(!$mail->Send())
{
    echo 'There was a problem sending this mail!';
}
else
{
    echo 'Mail sent!';        
}
$mail->SmtpClose();

答案 3 :(得分:2)

我认为您的问题是$mail->SMTPAuth = false;

很难相信有ISP或SMTP提供商不需要身份验证,即使它们是免费的。

除了检查send()之外,您可以尝试检查错误:

  if ( $mail->IsError() ) { // 
    echo ERROR;
  }
  else {
    echo NO ERRORS;
  }

 //Try adding this too, for debugging:
  $mail->SMTPDebug  = 2;  // enables SMTP debug information

代码中的其他所有内容都很好。我们经常使用PHPMailer并且从未遇到任何问题

答案 4 :(得分:1)

可能位于您省略的部分。这两封电子邮件的发件人域名是否相同?否则,SMTP主机可能会将此视为中继尝试。如果您有权访问SMTP服务器日志,请检查这些日志;他们可能提供线索。

另外,请检查$mail->ErrorInfo说的内容......它可能会告诉您问题所在。

答案 5 :(得分:1)

我个人会尝试做一些小步骤,比如发送相同的电子邮件..所以只需清除收件人并尝试发送相同的电子邮件(此代码适用于我)。如果此代码通过,您可以继续添加以前的行并调试失败的地方

也许$mail->ClearCustomHeaders();在做问题

//SMTP servers details
$mail->IsSMTP(); 
$mail->Host = "mail.hostserver.com";  
$mail->SMTPAuth = false;     
$mail->Username = $myEmail;  // SMTP usr
$mail->Password = "****";    // SMTP pass
$mail->SMTPKeepAlive = true;   
$mail->From = $patrickEmail; 
$mail->FromName = "***";    
$mail->AddAddress($email, $firstName . " " . $lastName); 
$mail->WordWrap = 50;                                 
$mail->IsHTML(true);                                  
$mail->Subject = $client_subject;
$mail->Body    = $client_msg;
// all above is copied
if($mail->Send()) {
  sleep(5);
  $mail->ClearAllRecipients(); 
  $mail->AddAddress('another@email.com'); //some another email

}
...

答案 6 :(得分:1)

我认为你在这里遇到了组织问题。

我建议:

  1. 设置您的设置(SMTP,用户,通行证)
  2. 使用包含消息和地址的数组中的信息创建新的电子邮件对象
  3. 发送电子邮件
  4. 转到第2步

答案 7 :(得分:1)

尝试使用以下示例。

<?php

//error_reporting(E_ALL);
error_reporting(E_STRICT);

date_default_timezone_set('America/Toronto');

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port       = 26;                    // set the SMTP port for the GMAIL server
$mail->Username   = "yourname@yourdomain"; // SMTP account username
$mail->Password   = "yourpassword";        // SMTP account password

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address1 = "whoto@otherdomain.com";
$address2 = "whoto@otherdomain.com";

$mail->AddAddress($address1, "John Doe");
$mail->AddAddress($address2, "John Peter");

$mail->AddAttachment("images/phpmailer.gif");      // attachment if any
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment if any

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

注意:更好的是,您可以将多用户电子邮件和名称设为ARRAY,例如

<?php

    $recipients = array(
       'person1@domain.com' => 'Person One',
       'person2@domain.com' => 'Person Two',
       // ..
    );

    foreach($recipients as $email => $name)
    {
       $mail->AddCC($email, $name);
    }

    (or)

    foreach($recipients as $email => $name)
    {
       $mail->AddAddress($email, $name);
    }
?>

我认为这可以帮助您解决问题。