looping phpmailer
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++;
}
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() {
}
if you look through the php mailer code, there is this useful method ClearAllRecipients() if you want to clear to, cc, and bcc all at once.
You need a:
$mail=new PHPMailer()
in the beginning of your for
loop -as it is, the second time through it just messes around with the first email (since a new one isn't created).
As you pointed out body also needs to be reset - in fact using a separated var like that isn't very helpful - better to just supply directly to MsgHTML
. Since the content of your email is trivial you may also want to send a plain-text version of the data (depends on your target recipient I guess).
So the updated script:
foreach($array_values as $mail_vars)
{
$mail=new PHPMailer();
$mail->SetFrom('email', 'FULLNAME');
$mail->AddAddress($mail_vars[2], 'sss');
$mail->Subject = "NEW";
$mail->MsgHTML("<br>\nUsername: ".$mail_vars[0]."<br>\nPassword: ".$mail_vars[1]."<br>");
//$mail->Send();
$f++;
}
精彩评论