Reputation: 27
I am using PHP Mailer function to send mail. In order to send mail to multiple recipients, I am using for loop as per below :-
for($i=0;$i<count($com_string_array);$i++)
{
$soc_memb_comm_info = explode(",",$com_string_array[$i]);
$mailing->send_mail_with_attachment($soc_memb_comm_info[4],
$soc_memb_comm_info[3],$upload_path,$file_name,$message);
}
public function
send_mail_with_attachment($to,$full_name,$file_path,$file_name,$message)
{
//Other configuration parameter of PHP Mailer.
$this->AddAddress($to);
$this->Subject = "Welcome ";
$this->Body = $message;
$this->AltBody = $email_msg;
$this->AddAttachment($file_path,$file_name);
if(!$this->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $this->ErrorInfo;
exit;
}
My issue is that mail is going multipile times to same reciepient. For eg. if I am sending mail to three people then first person is getting mail three times. 1. Directly to First Person. 2. Directly to First Person, second person. 3. Directly to First Person, Second Person and Third Person.
Kindy advises why such issue is coming.
Upvotes: 1
Views: 1641
Reputation: 5766
The mailer you use is not reset each time you use the function to send. So when you call the send function the second time, the first recipient is still there, as well as the first attachment. You did not mention the attachments but i guess in the second mail the attachment will be there twice.
Either you have to use a new instance of PHPMailer or you can clear all recipient and attachments before you send a mail.
$this->clearAllRecipients();
$this->clearAttachments();
Upvotes: 3