Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate/Multiple emails sent from one email address #123

Closed
noeldesigns opened this issue Oct 17, 2013 · 6 comments
Closed

Duplicate/Multiple emails sent from one email address #123

noeldesigns opened this issue Oct 17, 2013 · 6 comments

Comments

@noeldesigns
Copy link

I am using the SMTP mailer with the include file: include 'class.phpmailer.php';

The parameters were changed for my hosting service and port.

I loop through the database to get email addresses and for a test I selected (3) email addresses. Using echo statements, the loop is processing correctly and sends emails.

The issue is that it sends email to each email address multiple times.

The loop appears to be working correctly as I have tested it with a $count++ to see how many times the loop is actually executing, and the count is correct.

Here is the code that I use for the loop and the mail. Is there something in the mail server that sends multiple emails.

$get_emails = send_email_all_contacts($start_id, $end_id);
//print_r($get_emails);
foreach($get_emails as $email) {

$user_id = $email['id'];    
$first_name = $email['first_name'];
$last_name = $email['last_name'];
$email = $email['email'];
$to = $email;
$send_to_name = $first_name;

$subject = $email_title;
$body = $email_text;
//Set who the message is to be sent from
$mail->SetFrom('changed to actual address', 'changed to actual name');
//Set an alternative reply-to address
//$mail->AddReplyTo('changed to actual email address', 'changed to actual name');
//Set who the message is to be sent to
$mail->AddAddress($to, $send_to_name);
//Set the subject line
$mail->Subject = $email_title;
//Read an HTML message body from an external file, convert referenced images to embedded, convert HTML into a basic plain-text alternative body
//$mail->MsgHTML(file_get_contents('contents.html'), dirname(FILE));
//Replace the plain text body with one created manually
$mail->Body = $email_text;
$mail->AltBody = $email_text;
//Attach an image file
//$mail->AddAttachment('images/phpmailer_mini.gif');

//Send the message, check for errors
if(!$mail->Send()) {
echo 'Emails did not get sent to this address', '
', $user_id,'
', $first_name, '
', $email, '
' ;
} else {
//echo 'Emails sent to this address', '
', $user_id,'
', $first_name, '
', $email, '
' ;
}

$count++;
echo 'number of times script executed', $count,'
';
}

Thanks

Grant

@Synchro
Copy link
Member

Synchro commented Oct 18, 2013

I'd suspect your mail server - I've had both gmail and hotmail do this without any involvement from PHPMailer. Look at the source of the received messaged and look at their mesage IDs, and also look at your mail server logs. You can also enable SMTP debug output to get a transcript of the SMTP conversations.

@noeldesigns
Copy link
Author

Thanks for the feedback. There are other issues as well. When an email is sent through the loop, the 1st email should go to person 1, the 2nd to person2, etc. However what is happening is that the first email is going to person1, the 2nd email is going to both person1 and person2, the 3rd email is going to person 1, person 2 and person 3.

To test, I set the $to field back to blank before sending the next email, but this doesn't seem to help.

Any suggestions are welcome.

Thanks,

Grant

@Synchro
Copy link
Member

Synchro commented Oct 18, 2013

You need to call clearAddresses() between messages, otherwise you're adding an address each time round the loop. There are similar functions for clearing CC, BCC etc too.
Don't include the classes directly - there is an autoloader that will work better:
require 'PHPMailerAutoload.php';

@noeldesigns
Copy link
Author

Thanks, I will definitely do that. I think the PHPMailerAutoLoad.php must have come out after I downloaded the version that I have. Quick question. If I included the autoload, do I also need to include the classes or does the autoload do that. Thanks, Grant

@Synchro
Copy link
Member

Synchro commented Oct 18, 2013

It was introduced quite recently. No, you don't need to load the classes, that's what autoloaders are for - you load the one file and it takes care of any others you need.

@Synchro Synchro closed this as completed Oct 18, 2013
@noeldesigns
Copy link
Author

Thanks for all your help. Grant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants