Occasionally Get Duplicate Emails from mail()

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I'm trying to spam myself :-\ with 2,000 emails, shooting for 15,000,
and I will occasionally get two identical emails. I'm putting a
$counter++ as the from and part of the subject line so I can see
what's going on. Any reason why this might happen? here's my code just
in case.


function send_mail($emailaddress, $fromaddress, $emailsubject, $body,
$method) {
   global $counter;

   # Common Headers
   if ($method == "text") {
      $headers  = "MIME-Version: 1.0\n";
      $headers .= "Content-type: text/plain; charset=iso-8859-1\n";
      $headers .= "X-Priority: 3\n";
      $headers .= "X-MSMail-Priority: Normal\n";
      $headers .= "X-Mailer: php\n";
   } elseif ($method == "html") {
      $headers  = "MIME-Version: 1.0\n";
      $headers .= "Content-type: text/html; charset=iso-8859-1\n";
      $headers .= "X-Priority: 3\n";
      $headers .= "X-MSMail-Priority: Normal\n";
      $headers .= "X-Mailer: php\n";

   $from = $counter++;

   $msg = $body;
   $msg = wordwrap($msg, 70);

   $emailsubject .= " :: " . $from;

   mail($emailaddress, $emailsubject, $msg, $headers);

$message = "<h2>Hello World</h2>";
$subject = "Test Email";
$to = "myemail@address.com";

$fromaddress = "";

for ($x=0; $x<2000; $x++) {
   send_mail($to, $fromaddress, $subject, $message, "html");

Re: Occasionally Get Duplicate Emails from mail()

Ok. aside from the wierdness that outlook received the same email
twice (got 4,000 in my inbox) my gmail account gladly swollowed the
2,000 correctly. But after further thought on my problem, I'm going to
say that PHP buffered all the requests to mail, and the counter
function wasn't keeping up with my global counter so it looked like I
was getting duplicate emails. When PHP got around to processing the
actual mail, the counter had already up'ed itself and simply said


Quoted text here. Click to load it

Site Timeline