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

Posted on 2003-12-02
Last Modified: 2008-02-01
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?
Question by:tawraven
  • 7
  • 5

Expert Comment

ID: 9858687
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.

Author Comment

ID: 9858753
no duplicates

Author Comment

ID: 9858878
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.
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


Expert Comment

ID: 9859062
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.

Author Comment

ID: 9859184
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.

Expert Comment

ID: 9859206
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.

Expert Comment

ID: 9859214
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.  


Author Comment

ID: 9859291
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.

Author Comment

ID: 9859430
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.

Author Comment

ID: 9859710
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.

Expert Comment

ID: 9859756
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.

Accepted Solution

petoskey-001 earned 125 total points
ID: 9859862
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.

Author Comment

ID: 9860491
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.

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP warning 4 40
How can I change this hyperlinked phone number to be black and is it working? 2 47
PHP populating an array. 4 22
Cookie not unsetting 7 18
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now