Solved

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

Posted on 2003-12-02
13
299 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
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…

733 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