【问题标题】:Sending 2 emails with PHP mailer fails使用 PHP 邮件程序发送 2 封电子邮件失败
【发布时间】:2012-11-13 17:11:46
【问题描述】:

我对这个很困惑。

//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())
{
...

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


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

【问题讨论】:

  • 删除$mail->ClearCustomHeaders(); 试试看....
  • @Baba 谢谢,但没有骰子。没有什么不同。
  • 我不推荐使用 PHPMailer。编写自己的简单邮件类将是一种更多的实践经验,并且错误更少。我抛弃了 phpmailer 类,因为它带有大量错误和非常愚蠢的错误......
  • @Cobra_Fast 你能举出错误和愚蠢错误的例子吗?

标签: php email phpmailer


【解决方案1】:

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

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

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

【讨论】:

  • 感谢您的建议,值得一试。这个周末我会去。
  • 如果你的 php 设置的执行时间设置是默认的(30 秒),当计时器用完时,它将停止执行。
  • @JamesMcDonnell 你是对的;谢谢你说清楚。现在我编辑了我的答案,将睡眠时间从 30 秒减少到 10 秒。
  • 这为我解决了这个问题:我需要在另一封邮件中发送确认治疗 + 账单,这两个选项都在我的后台,谢谢!
【解决方案2】:

恕我直言,您需要为每封发送的电子邮件创建新的 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();
}

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      我认为你的问题是$mail-&gt;SMTPAuth = false;

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

      除了检查send() true 之外,您还可以尝试这个来检查错误:

        if ( $mail->IsError() ) { // 
          echo ERROR;
        }
        else {
          echo NO ERRORS;
        }
      
       //Try adding this too, for debugging:
        $mail->SMTPDebug  = 2;  // enables SMTP debug information
      

      您的代码中的其他所有内容看起来都很好。我们经常使用 PHPMailer,从来没有遇到过任何问题

      【讨论】:

      • 最初设置为true。我将其更改为 false 以尝试,但忘记将其设置回来(两者都适用于第一封电子邮件,而不适用于第二封。
      • 删除 $mail-&gt;SMTPKeepAlive = true; 并重试。
      【解决方案5】:

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

      另外,查看$mail-&gt;ErrorInfo 所说的内容...它可能会告诉您问题所在。

      【讨论】:

      • 是的,同一个域发件人。我无权访问 SMTP 日志。对于 $mail->ErrorInfo,我需要修改我的脚本(通过 Ajax 调用)
      【解决方案6】:

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

      也许$mail-&gt;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
      
      }
      ...
      

      【讨论】:

        【解决方案7】:

        试试下面的例子,

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

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

        【讨论】:

          【解决方案8】:

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

          我推荐:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-06-10
            • 2017-12-08
            • 1970-01-01
            相关资源
            最近更新 更多