Socketmail function sends two copies of email...need help.

i found a socketmail function on that works great but it sends two copies of an email.  here is the code:

function socketmail($toArray, $subject, $message) {
  // $toArray format --> array("Name1" => "address1", "Name2" => "address2", ...)

  ini_set(sendmail_from, "");

  $connect = fsockopen (ini_get("SMTP"), ini_get("smtp_port"), $errno, $errstr, 30) or die("Could not talk to the sendmail server!");
   $rcv = fgets($connect, 1024);

  fputs($connect, "HELO {$_SERVER['SERVER_NAME']}\r\n");
   $rcv = fgets($connect, 1024);

  while (list($toKey, $toValue) = each($toArray)) {

   fputs($connect, "MAIL\r\n");
     $rcv = fgets($connect, 1024);
   fputs($connect, "RCPT TO:$toValue\r\n");
     $rcv = fgets($connect, 1024);
   fputs($connect, "DATA\r\n");
     $rcv = fgets($connect, 1024);

   fputs($connect, "Subject: $subject\r\n");
   fputs($connect, "From: Engineering <>\r\n");
   fputs($connect, "To: $toKey  <$toValue>\r\n");
   fputs($connect, "X-Sender: <>\r\n");
   fputs($connect, "Return-Path: <>\r\n");  
   fputs($connect, "X-Mailer: PHP\r\n");
   fputs($connect, "X-Priority: 3\r\n");
   fputs($connect, "Content-Type: text/html; charset=iso-8859-1\r\n");
   fputs($connect, "\r\n");
   fputs($connect, stripslashes($message)." \r\n");

   fputs($connect, ".\r\n");
     $rcv = fgets($connect, 1024);
   fputs($connect, "RSET\r\n");
     $rcv = fgets($connect, 1024);

  fputs ($connect, "QUIT\r\n");
   $rcv = fgets ($connect, 1024);

To get my $toArray value, I do a query to database and put results into array as shown below:

$userQuery = "SELECT firstName, lastName, email FROM users WHERE department = 'department'";
$result = mysql_query($userQuery, $db);
$toArray = array();
while (list($firstname, $lastname, $email) = mysql_fetch_row($result)) {
      $temp = array(
      $firstname." ".$lastname => $email
      $toArray = array_merge($toArray, $temp);

it all works except for the duplicate email.  any suggestions?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

It looks straight forward.  I would try printing the array instead and see if there really are duplicates.

Try doing this instead of calling socketmail...

  print 'The current $toArray looks like this... <br>';

That should show you if your array really does have duplicates.  Then we will know if we should look at the socketmail or the array creation.
tawravenAuthor Commented:
no duplicates
tawravenAuthor Commented:
to be more informative about it, i set up every name in the database to have the same email (mine) for testing purposes.  But, for example, only 3 people match the department but i get six emails.  What i am saying is that there are no duplicate names in the array but i get two emails with the same persons name on it.
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Try putting an echo command into the loop that sends the mail,

Something like

echo "sending mail to $toValue";

And let us know the output.
tawravenAuthor Commented:
there are 4 people in the department and it says "sending mail to" 4 times.  basically it's going thru the loop the right number of times.

i read somewhere that if you have a "To:" line in your headers that it will send two copies but i don't really understand how this socketmail function works to know if i am doing that.
Well I downloaded it to my machine and tested it and I do not get duplicates.  I don't see anywhere in the script where there could be duplicates, except in the array creation or in the database (which is pretty much the same thing).

So the other possibility is that the whole page itself is being called twice.  I added this line to socketmail right after the while(list($toKey)... line...

   print "<b>Sending Email to $toKey at $toValue</b><br>"; flush();
    $counter = $counter + 1;

then later on do this...

//   fputs($connect, "Subject: $subject\r\n");
   fputs($connect, "Subject: Email $counter - $subject\r\n");

This should tell you the order that the emails were generated in case there is a server problem.
DUH!! that's it!

You don't need to - you already have RCPT-TO during your initial connection.  Adding the TO line below sends another copy.  

tawravenAuthor Commented:
petoskey....i commented out that line and when the email comes in, there is nothing in the To: field on the email and i still get doubles.  don't think that's it.
tawravenAuthor Commented:
i tried your suggestion with the counter and now the first email gets generated twice and the rest of them only once.  i'm so confused....could it be how outlook downloads them from the server?  maybe it is downloading 2 copies or something....just a shot.
tawravenAuthor Commented:
this is weird....i put your counter into the works fine.  i take the counter out, it generates doubles.  i get different results everytime it seems.
OK, my guess is there is something happening you are not aware of.

Add this line somewhere early on in your script.


See this link for more info...

This will probably display several errors or warnings that you are not aware of at the moment.  The only other possiblity is that your whole script is being called more then once, but I doubt that.
The only other thing about my counter that may be of interest is the flush() command.  Flush tells PHP to send anything in it's output buffers.  So perhaps flushing the fputs command is making sure that your telnet commands are being sent correctly.  Perhaps something is dropping characters.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tawravenAuthor Commented:
i don't know what is causing it but when i leave flush() in there with the original works fine.  i just don't understand why you tried the code and you didn't get duplicates.  thanks for all your help though.  Points go to Petoskey-001.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.