Hi all,

I've just finished a PHP/MySQL mailing list. Basically, I'm having problems
with my sendmail function. It takes an array of e-mail addresses
($addresses) and loops through it, e-mailing each one using an SMTP class I
found (the only identifying comment in it is "SMTP Class By TOMO
(2001/09/14)"). The problem is, it's very slow. The typical array of
addresses sent to this is 100 to 500 elements large. The actual mailing list
has over 7000 members, but the e-mails are sent out to a geographical
region. Ideally, though, this function should be able to handle mailing to
the entire member base.

Here's the offending function (with private stuff altered). Currently, it
will only send to about 50 of the addresses before Internet Explorer times
out and cuts it off (perhaps five minutes or so). Note that this is without
catches, so it skips to the 'else'.

If I comment out the sending part, as shown, the operation takes a split
second and the log is written correctly, so I know the problem lies in the
sending of the mail. The only solution I can think of is to make the user
send things in chunks of 50 or so addresses, but I'd like to avoid this
inconvenience if I can. Any ideas?

// Sends e-mail to the specified list of member e-mail addresses.
function ncSendMail($addresses, $subject, $body)
{
   // Delete last log file.
   $ftp = ftp_connect("ftp.somewhere.net");
   ftp_login($ftp, "someone", "something");
   ftp_delete($ftp, "/sendlog.txt");
   ftp_quit($ftp);

   // Create new log file.
   $sendlog = fopen("ftp://someone:[EMAIL PROTECTED]/sendlog.txt";,
"w");
   fwrite($sendlog, "Last Mailing Result\r\n");
   fwrite($sendlog, "Generated: ".date("m-d-Y, h:i:sa")."\r\n\r\n");
   fwrite($sendlog, "The e-mail attempted is reproduced below.\r\n");
   fwrite($sendlog, "Subject: ".$subject."\r\n");
   fwrite($sendlog, $body."\r\n\r\n\r\n");

   // Get the automail 'footer' from the 'automail' table and append it to
the body.
   $body .= ncGetAutomail("footer");

   // If there are catches, then for each member in $addresses, parse the
subject and body for catches and substitute where appropriate.
   if ( is_string(strstr($body, "%FIRSTNAME%")) ||
        is_string(strstr($subject, "%FIRSTNAME%")) ||
        is_string(strstr($body, "%LASTNAME%")) ||
        is_string(strstr($subject, "%LASTNAME%")) ||
        is_string(strstr($body, "%EMAIL%")) ||
        is_string(strstr($subject, "%EMAIL%")) )
   {
      $mysql = ncConnect();
      fwrite($sendlog, "Beginning to send messages...\r\n\r\n");
      foreach($addresses as $address)
      {
         $this_subject = $subject;
         $this_body = $body;
         $dbresult = mysql_unbuffered_query("SELECT lname, fname FROM
members WHERE email = '$address'");
         $dbrow = mysql_fetch_row($dbresult);
         $this_subject = str_replace("%FIRSTNAME%", $dbrow[1],
$this_subject);
         $this_subject = str_replace("%LASTNAME%", $dbrow[0],
$this_subject);
         $this_subject = str_replace("%EMAIL%", $address, $this_subject);
         $this_body = str_replace("%FIRSTNAME%", $dbrow[1], $this_body);
         $this_body = str_replace("%LASTNAME%", $dbrow[0], $this_body);
         $this_body = str_replace("%EMAIL%", $address, $this_body);

         $smtp = new smtp("smtp.somewhere.com");
         if ($smtp->sendmail($address, "[EMAIL PROTECTED]",
$this_subject, $this_body))
            fwrite($sendlog, "Successfully sent to member
\"".$address."\".\r\n");
         else
            fwrite($sendlog, "Failed to send to member
\"".$address."\".\r\n");
      }
      fwrite($sendlog, "\r\nFinsihed sending messages.");
      mysql_close($mysql);
   }

   // If there aren't catches, send the plain e-mail.
   else
   {
      fwrite($sendlog, "Beginning to send messages...\r\n\r\n");
      foreach($addresses as $address)
      {
         //$smtp = new smtp("smtp.domain-mail.com");
         //if ($smtp->sendmail($address, "[EMAIL PROTECTED]", $subject,
$body))
            fwrite($sendlog, "Successfully sent to member
\"".$address."\".\r\n");
         //else
         //   fwrite($sendlog, "Failed to send to member
\"".$address."\".\r\n");
      }
      fwrite($sendlog, "\r\nFinished sending messages.");
   }

   // Return.
   fclose($sendlog);
   return true;
}



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to