Jump to content

Recommended Posts

I am using PHP Mailer to send out emails to an array of email address. So I am looping through the array of emails and then sending out the emails individually. The script works except it is sending out more than one email to some of the email address.

Today my client told me that he got 2 more emails on Sunday just like the 2 he got on Thursday. I am assuming that this has something to do with caching, but I do not know.

 

The trigger that sends out the emails is part of a cron job. The cronjob runs every 30 minutes. Checks to see if there is a new entry in one of the tables in the database. Then it sends out the email to all of the members. After the email is sent a value is changed for that entry in the data base. I am explaining this only to help you to see that the problem is not that the code is being triggered again. If that was the problem then there would be and email every 30 minutes and that is not what happened.

 

Here is the code:

 

 
<?php
require_once('library/PHPMailer/class.phpmailer.php');
require_once('library/PHPMailer/class.smtp.php');    

foreach($organizationemails as $key => $value){
        $contents = '<body>
                                <div>
                                <p>
                                ...content of email ...
                                </p>
                                </div>
                                </body>';


        error_reporting(E_ALL);
        error_reporting(E_STRICT);
        $mail             = new PHPMailer();
        $body             = $contents;
        $body             = preg_replace('/[\]/','',$body);
        $mail->IsSMTP();
        $mail->Host       = "ssl://smtp.mysite.org";
        $mail->SMTPAuth   = true;                 
        $mail->Port       = 465;                  
        $mail->Username   = "sendingmail@mysite.org";
        $mail->Password   = "password";       
        $mail->SetFrom('info@mysite.org', 'My Organization');
        $mail->AddReplyTo('info@mysite.org', 'My Organization');
        $mail->Subject    = 'My Email Title';
        $mail->AltBody    = '... has been posted...';
        $mail->MsgHTML($contents);
        $address = $value;
        $mail->AddAddress($address);
        
        if(!$mail->Send())
        {
             echo "Message could not be sent. <p>";
             echo "Mailer Error: " . $mail->ErrorInfo;
             exit;
        }else{
            echo "Message has been sent to ".$value;    
        }
}
?>
Link to comment
https://forums.phpfreaks.com/topic/288935-php-mailer-sending-out-multiple-emails/
Share on other sites

This is not near enough information to help with anything but a guess.

Obvious questions:

 

  • Are you sure there aren't multiple entries in the database for the same email?
  • Are you logging the messages to a file?  What does the file indicate? For these dupes?
  • Are you sure the cron job setup is correct?
  • Did you check the logs for the MTA that is actually sending the emails?

That small code snippet doesn't tell us anything. Could be an issue with the query to fetch the pending mails (which we don't know), could be an issue with your server configuration (which we don't know), could be an issue with your PHPMailer version (which we don't know).

 

Make sure you got an up-to-date version of PHPMailer (5.2.7). Then run the script a couple of times and send yourself some emails to see if you can reproduce the problem. If you can, var_dump() will help you analyze the exact conditions.

Edited by Jacques1
  • Are you sure there aren't multiple entries in the database for the same email?

Yes, I am sure there are not multiple emails. I tweeked the code to echo the emails so I could read the list of emails. So, I am sure this is not the cause of the problem.

  • Are you logging the messages to a file?  What does the file indicate? For these dupes?

No, I am not logging the messages to a file. I can set that up and see what is said there

  • Are you sure the cron job setup is correct?

Yes, I am very positive the cron job is set up correctly. I has been running for over a year using the php mail() function. This never produced a duplicate email. The problem with the php mail() function is that some of the members of this organization were not getting an email because of the security settings in their email client.

  • Did you check the logs for the MTA that is actually sending the emails?

Not sure what you mean by this.

 

  • Could be an issue with the query to fetch the pending mails (which we don't know),

The query has been tested and checked. The problem is not the query. I would give you the code for the query but it is complicated and I have tested it alot of times. I have  I have run the code and instead of sending the email, I echoed out the values of $organizationemails  array. So, I am sure that there is not a problem with that end of the code

  • could be an issue with your server configuration (which we don't know).

This seems the most likely to be the problem. The server is a GoDaddy Virtual Dedicated Server, Plesk Paralles Panel.

 

  • Make sure you got an up-to-date version of PHPMailer (5.2.7). Then run the script a couple of times and send yourself some emails to see if you can reproduce the problem. If you can, var_dump() will help you analyze the exact conditions.

I have been using 5.2.4.  I will update to a the newest version. I did run a lot of tests on the script. I have tested the script by sending it out to 3 of my email address. (Editing the query to querry a different table in the database. That only has my email address in it.) I have not been able to duplicate the problem when I am only sending to my three email address's. The problem has only occurred when I send out to the actual list that is about 180 emails.

The "MTA" is the mail transfer agent, aka, the actual email server that is sending the email.  Typically on a unix server, this is going to be sendmail, postfix, exim, qmail or one of a few others.  

 

PHPMailer is transferring the mail to the MTA which then makes the actual SMTP connections to the delivery servers.

 

Those logs are much more authoritative.

 

I see however, that you are using GoDaddy?  I assume then that you are using GoDaddy's mail servers as the actual MTA?  Ugghhh.  

 

GoDaddy has a lot of confusing issues associated with their mail server infrastructure and delivery or lack-thereof.  

Yes, I am using a server on GoDaddy. The server that is sending the email is a Virtual Dedicated Server hosted with GoDaddy.  I think that it is different than having shared hosting with GoDaddy, because, to the best of my knoweledge, I ham not using GoDaddy's mail server. I really do not know much about servers and there are alot of settings in the c-panel for the server that I just do not understand.

In my gut I feel like the problem is a cashing issue.

I have tried multiple tests that send to my 3 emails and have not been able to recreate the problem. My client was a bit upset about the multiple emails going out to some of the members so I have been a bit reluctant to ask him if I can run another test so that I can log the messages to a file, but I think I have no other choice.

Thanks for all of your help.

Thanks also for telling me that GoDaddy has confusing issues associated with their mail server infrastructure and delivery or lack-thereof. This will help me out with another issue I am having with another site.

I have experience with this issue.

 

For me, the cause is due to my mailing list, while having unique email addresses (I make sure to de-dupe), there are instances where more than one email address belongs to one person - a main address and any number of alternate addresses.

 

The customer, knowingly or absent-mindedly, forwards all the alternate addresses to the main address. In forwarding, some email managers happen to mangle the headers so that the original address is replaced with the main address - thus no way to trace back the To: or Envelope For: headers - if the recipient knows how to get at that info.

 

I had to add boilerplate at the end of the email body to positively identify the email address that the customer wanted to have removed. (That said, a professional mailer service, such as Constant Contact, will have all the auto-unsubscribe data in the email.)

There is no mystery about the MTA.  You are setting it in your code:

$mail->Host       = "ssl://smtp.mysite.org";

We just don't know what server you are specifying there.

 

​I was under the impression that godaddy requires you to use their servers as relays:  http://support.godaddy.com/help/article/122/what-are-the-relay-mail-server-settings-on-my-dedicated-or-virtual-private-server?locale=en

Thank You bsmither. That is a good idea, but it is not what is happening here. I'm sure of it for a couple of reasons. First I have been using the same list for over a year, the emails were being sent using the php mail() function. This never produced duplicates. Second the people in this organization that I am in contact with are in a field that requires clerical precision. I'm sure they would know if this were happening. Truly they are very organized. 

 

Thank You gizmola I looked it up in my hosting account it says that my SMTP server name is dedrelay.secureserver.net. I have a meeting I have to go to. I will read over the documentation you gave me a link to this afternoon.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.