Solved

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

Posted on 2003-12-02
13
289 Views
Last Modified: 2008-02-01
i found a socketmail function on php.net 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, "email@email.com");

  $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 FROM:email@email.com\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 <email@email.com>\r\n");
   fputs($connect, "To: $toKey  <$toValue>\r\n");
   fputs($connect, "X-Sender: <email@email.com>\r\n");
   fputs($connect, "Return-Path: <email@email.com>\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);
  fclose($connect);
  ini_restore(sendmail_from);
}


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?
0
Comment
Question by:tawraven
  • 7
  • 5
13 Comments
 
LVL 7

Expert Comment

by:petoskey-001
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>';
  print_r($toArray);

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.
0
 

Author Comment

by:tawraven
ID: 9858753
no duplicates
0
 

Author Comment

by:tawraven
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.
0
 
LVL 6

Expert Comment

by:aolXFT
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.
0
 

Author Comment

by:tawraven
ID: 9859184
there are 4 people in the department and it says "sending mail to mail@mail.com" 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.
0
 
LVL 7

Expert Comment

by:petoskey-001
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.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 7

Expert Comment

by:petoskey-001
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.  



0
 

Author Comment

by:tawraven
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.
0
 

Author Comment

by:tawraven
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.
0
 

Author Comment

by:tawraven
ID: 9859710
this is weird....i put your counter into the loop....it works fine.  i take the counter out, it generates doubles.  i get different results everytime it seems.
0
 
LVL 7

Expert Comment

by:petoskey-001
ID: 9859756
OK, my guess is there is something happening you are not aware of.

Add this line somewhere early on in your script.

error_reporting(E_ALL);

See this link for more info...
http://us2.php.net/error_reporting

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.
0
 
LVL 7

Accepted Solution

by:
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.
0
 

Author Comment

by:tawraven
ID: 9860491
i don't know what is causing it but when i leave flush() in there with the original code...it 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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 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

15 Experts available now in Live!

Get 1:1 Help Now