Sylver
Sylver

Reputation: 8967

Sending 2 emails with PHP mailer fails

I am rather puzzled with this one.

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

The first email sends fine. The second one doesn't. What could be the reason for that behavior? Am I missing some kind of reset?


Update: using a different mail server seems to work so apparently it's a setting of that specific mail server causing problems. Any idea what that could be?

Upvotes: 6

Views: 13090

Answers (8)

Michael J. Calkins
Michael J. Calkins

Reputation: 32645

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

Upvotes: 1

user1419445
user1419445

Reputation:

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.

Upvotes: 9

John Peter
John Peter

Reputation: 2928

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('[email protected]', 'First Last');

$mail->AddReplyTo("[email protected]","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 = "[email protected]";
$address2 = "[email protected]";

$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(
       '[email protected]' => 'Person One',
       '[email protected]' => '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.

Upvotes: 1

Vikram
Vikram

Reputation: 4190

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

Upvotes: 5

mychalvlcek
mychalvlcek

Reputation: 4046

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('[email protected]'); //some another email

}
...

Upvotes: 1

Felipe Alameda A
Felipe Alameda A

Reputation: 11799

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

Upvotes: 2

Michal Čihař
Michal Čihař

Reputation: 10091

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

Upvotes: 3

fbitterlich
fbitterlich

Reputation: 913

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.

Upvotes: 1

Related Questions