Samir Karmacharya
Samir Karmacharya

Reputation: 890

$mail->Send() send multiple email using PHPMailer class

using PHPMailer class. I have following code. for email send to client and owner same email, only Send from information is change for client and owner. Blockquote when i submit email information ,client get double email(get 2 email) and owner get 1 email. and client get email from client information how can send individual email to both. is this code contain any error?

/* for client email send*/   
    $emailAddr ='[email protected]';
         $body             = $client_message;
            $body             = eregi_replace("[\]",'',$body);    
            $mail->SetFrom(c, $name);    
            $mail->AddAddress($_POST['email'], $_POST['name']);    
            $mail->Subject    = "subject1";    
            $mail->MsgHTML($body);
            $mail->AddAttachment("images/download.pdf"); 



if(!$mail->Send()) {
      echo "Mailer Error: " . $mail->ErrorInfo;
    }
    /*For owner email*/    
    $client_message1 = $client_message;
    $body             = $client_message1;
    $body             = eregi_replace("[\]",'',$body);
    $mail->SetFrom($_POST['email'], $_POST['name']);    
    $mail->AddAddress($emailAddr, $name);    
    $mail->Subject    = "subject1";
    $mail->MsgHTML($body);

    if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
    }

Upvotes: 1

Views: 445

Answers (1)

Usman Ghani
Usman Ghani

Reputation: 46

Note: Since you didn't mention it in your question, I am assuming that you are using PHPMailer class. It would be a good idea to update the question with this information.

The AddAddress() function, as the name implies, adds new addresses to the recipients list while at the same time keeping the previously added addresses.

Solution:

You'll have to use clearAllRecipients() function before you added the recipient address for the second email at the line $mail->AddAddress($emailAddr, $name);. Your final code in that part should look like this:

$mail->clearAllRecipients(); $mail->AddAddress($emailAddr, $name);

Ref: http://phpmailer.github.io/PHPMailer/classes/PHPMailer.PHPMailer.PHPMailer.html#method_clearAllRecipients

There are many other similar useful functions mentioned there. Please check. Also, have a look into this question and the answer. It will help you understand the problem more.

Hope my answer helps.

Upvotes: 3

Related Questions