Jeff Jeff - 20 days ago 5
PHP Question

PHPMailer sending email to wrong recipient

I have a situation where I sending an 2 different emails, one to a customer and one to a member after the member accepts a task. Within the function I am calling 2 functions, one that sends to the client and one is sent to the member. The what is happening is that the second email is being sent to both the member and the client, yet the client, the first email, only gets the email expected.

What I figured is happening is that because both of these functions are occurring at the same time, some how the addAddress() function is taking both email addresses over to the second email. My proof of this is I changed the order of the functions so the Member send first and the Client sent second and they both got the Client email where before the Client sent first and the Member sent second.

My question than is how do I make the first email break for a few seconds before the second email is sent to stop this from happening.

Here is the basic set up of the code.

function memberAccept()
{
// SQL to mark in DB that member accept task

$this->memberEmail($id)
$this->clientEmail($id)
}

function memberEmail($id)
{
// SQL to gather information about member inner join with task

$subject = 'Email';
$body = 'Email to member'
$this->sendEmail($member['email'], $subject, $body)
}

function clientEmail($id)
{
// SQL to gather information about client inner join with task

$subject = 'Email';
$body = 'Email to client'
$this->sendEmail($client['priemail'], $subject, $body)
}


sendEmail() is in another Class set up to use PHPMailer functions to send the email.

Answer

In your case, you must use the following function before adding new recipient for the mail:

$mailer->ClearAllRecipients( ); // clear all

Basically, the problem is that, when you add first recipient, mailer successfully delivers the mail, but the object is not destroyed yet. Hence, when the next mail is sent, member's email is still in the recipient, so alter your code to call this ClearAllRecipients function before adding the new recipient.

Comments