Sending 2 emails with PHP mailer fails

跟風遠走 提交于 2019-12-05 10:29:47

Some providers impose restrictions on the number of messages that can be sent within a specific time span. To determine if your problem depends by a provider "rate limit", you should try to add a pause after the first send. For example:

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

Then, by progressively lowering the sleep time, you should be able to determine which is the rate limit.

IMHO you need to create new PHPMailer object for every sent email. If you want to share some common setup, use something like this:

$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();
}

Try this: As @Felipe Alameda A mentioned Remove $mail->SMTPKeepAlive = true;

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

I think your problem is $mail->SMTPAuth = false;

It is hard to believe there are ISP or SMTP providers that don't require authentication, even if they are free.

You may try this to check for errors instead of or in addition to checking for send() true:

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

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

Everything else in your code looks fine. We use PHPMailer a lot and never had any problems with it

The key may lie in the parts you have omitted. Is the domain of the sender of both emails the same? Otherwise the SMTP host may see this as a relay attempt. If you have access to the SMTP server logs, check these; they might offer a clue.

Also, check what $mail->ErrorInfo says... it might tell you what the problem is.

i personally would try to make small steps like sending same email.. so just clear recipients and try to send identical email (this code works for me). If this code passes you can continue to adding back your previous lines and debug where it fails

and maybe $mail->ClearCustomHeaders(); doing problems

//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

}
...

Try with the following example.,

<?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!";
}
?>

Note : Better you can make a multiple user email and name as an ARRAY, like

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

i think this may help you to resolve your problem.

I think you've got organizational problems here.

I recommend:

  1. Set your settings (SMTP, user, pass)
  2. Create new email object with info from an array holding messages and to addresses
  3. Send email
  4. Goto step 2
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!