Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

looping phpmailer

Tags:

php

when i send email i get two emails but it should send email to respective emails. Lopoping problem ?

$array_values = Array
(
[0] => Array
(
[0] => uname1
[1] =>  fullname1
[2] => email 1


)
[1] => Array
(
[0] => uname2
[1] =>  fullname2
[2] => email 2


)
)
$f=0;
foreach($array_values as $mail_vars)
{

//$mail->AddReplyTo($mail_vars[2],'RED');
$mail->AddAddress($mail_vars[2], 'sss');
$body .="<br>";
$body .= 'Username: '. $mail_vars[0];
$body .="<br>";
$body .= 'Password: '.$mail_vars[1];
$body .="<br>";
$mail->SetFrom('email', 'FULLNAME');
$mail->Subject    = "NEW";
$mail->MsgHTML($body);
//$mail->Send();

$f++;
}
like image 259
mark rammmy Avatar asked Sep 16 '10 15:09

mark rammmy


1 Answers

Looking through the source of PHP Mailer, you will need to clear the fields. At least the address, maybe more. Here is the section of code from the PHPMailer class that has the clear functions. You are more than welcomed to look through them and try them etc. This is obviously an alternative to re-instantiating a new object, which may or may not cause a memory leak (depending on how many calls you make to it).

So implementing the clearAddresses code:

    $mail->Subject    = "NEW";
    $mail->MsgHTML($body);
    $mail->Send();
    $mail->ClearAddresses(); // should reset the To address and remove the first one from it.

I removed the actual code as you just need the description and function name.

    /////////////////////////////////////////////////
  // CLASS METHODS, MESSAGE RESET
  /////////////////////////////////////////////////

  /**
   * Clears all recipients assigned in the TO array.  Returns void.
   * @return void
   */
  public function ClearAddresses() {
  }

  /**
   * Clears all recipients assigned in the CC array.  Returns void.
   * @return void
   */
  public function ClearCCs() {
  }

  /**
   * Clears all recipients assigned in the BCC array.  Returns void.
   * @return void
   */
  public function ClearBCCs() {
  }

  /**
   * Clears all recipients assigned in the ReplyTo array.  Returns void.
   * @return void
   */
  public function ClearReplyTos() {
  }

  /**
   * Clears all recipients assigned in the TO, CC and BCC
   * array.  Returns void.
   * @return void
   */
  public function ClearAllRecipients() {
  }

  /**
   * Clears all previously set filesystem, string, and binary
   * attachments.  Returns void.
   * @return void
   */
  public function ClearAttachments() {
  }

  /**
   * Clears all custom headers.  Returns void.
   * @return void
   */
  public function ClearCustomHeaders() {
  }
like image 125
Jim Avatar answered Sep 29 '22 17:09

Jim