6 Replies - 2523 Views - Last Post: 12 December 2003 - 08:48 PM Rate Topic: -----

#1 graeder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 17-December 02

Problem With Php Email List

Posted 10 December 2003 - 05:30 PM

I have a php script which querys mySql and retrieves about 600 email addresses. The script then loops through each record and sends the appropriate mail. I built an email client whereby the user enters whatever they want for the message, hits "send", and calls the "mail" script I was just describing. My problem is that when the user hits send, the php script begins to run on the server (the client page is still displayed), but eventually the page just hangs and I get the dreaded "page not available" error.
Is there any way to avoid what seems to be a timeout? Here's the php code:

<html>
<head>
<style>
body{font-family:verdana,arial;font-size:11px;}
</style>
</head>
<body>
<?php
$content = $_REQUEST["form_submit_contents"];
$host = "localhost";
$user = "us54663";
$password = "\$X2ue7e5";
$DBName = "db54663a";
$send_check = true;
$element = array();


$link = mysql_connect($host,$user,$password)
or die("unable to connect to mysql");

$query = "SELECT * from customerList";

$result = mysql_db_query($DBName,$query,$link)
or die("unable to query mysql");

while ($row = mysql_fetch_array($result))
{
$total = array_push($element,$row[email]);

$to="$row[email]\n";
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: Smokejacks Restuarant <info@smokejacks.com>\n";
$headers .= "X-Sender: <info@smokejacks.com>\n";
$headers .= "Reply-To: info@smokejacks.com\n";
$headers .= "X-Mailer: PHP \n"; //mailer
$subject = "Smokejacks Update";
$body="<html><head></head><body>$content</body></html>";
// Now we send the message
//We set a variable equal to the mail() function in order to capture success or fail.
//mail($to,$subject,$body, "info@smokejacks.com", "-f info@simokejacks.com");
$send_check = mail($to,$subject,$body,$headers,"-f info@smokejacks.com");
}



if ($send_check!=true)
{
print("An error occurred while sending mail.");
die();
}

else
{
print("<br><br>Mail sent successfully to the following recipients:<br><br>");
foreach($element as $value)
{
print("$value<br>");
}
}
?>

I'm fairly new to php, so perhaps somebody has a suggestion on how to improve this code. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem With Php Email List

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Problem With Php Email List

Posted 10 December 2003 - 05:40 PM

Well, I didn't really scan through the code that much, but is the message the same for each mail recipient? If so, are you getting the timeout because you're sending 600 mails? If the message is the same, I would try building a concatenated list of the address and just sending it once.
Was This Post Helpful? 0
  • +
  • -

#3 graeder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 17-December 02

Re: Problem With Php Email List

Posted 10 December 2003 - 06:05 PM

Just have a comma separated concatenated list of addresses and bcc that list?
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Problem With Php Email List

Posted 10 December 2003 - 08:14 PM

Well, comma separated or semi colon (I'm not sure what the php mail function would take). You could put that list in the To: line, or CC: or BCC:, whatever you like. As long as you're only sending the mail once instead of calling the function every time you go through the loop. It's just a thought, may not work.

HTH!
:)
Was This Post Helpful? 0
  • +
  • -

#5 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1865
  • View blog
  • Posts: 20,278
  • Joined: 17-March 01

Re: Problem With Php Email List

Posted 11 December 2003 - 01:20 PM

I have a similar script here at d.i.c that sends to 3000 users
takes a good 45 minutes to an hour to do so however.

You have 2 options, to keep the page from timing out, I personally have it echo the email addresses as it sends them, so every 45-60 seconds it will display another 100 email addresses or so on the screen, it doesn't "stream" them per say, but it will send a chunk, then send the addresses to the browser.

The other thing I do is pause every 100 emails for 15 seconds so it has a chance to send them, longer you pause, the less emails get stuck in the mail queue, the shorter you pause, the more get stuck and the longer it can take to send them all because the queue only processes so many emails at any 1 time.

The second option, and one I have never personally used, but apparently you can use the smtp functionality on your host to send emails which is faster. No matter what you do, I would send an individual email to each address, not putting them in a list, reason being, some hosts (myself included) limit the numbers of recipients in any one email to a certain numbers, this would mean if you tried to send to say... 50 people... it wouldn't work, but every host is different.
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Problem With Php Email List

Posted 11 December 2003 - 03:22 PM

hmm... I hadn't even thought about the host limiting the number of recipients. Excellent point.
Was This Post Helpful? 0
  • +
  • -

#7 cyberscribe  Icon User is offline

  • humble.genius
  • member icon

Reputation: 10
  • View blog
  • Posts: 1,062
  • Joined: 05-May 02

Re: Problem With Php Email List

Posted 12 December 2003 - 08:48 PM

A radical alternative is to use Perl with mail::sendmail, shell script with the mail command (assuming UNIX) or any other form of scripting approach that streams output and/or doesn't have to keep the HTTP protocol happy while it's doing its job.

These are essentially better tools for the job, because they can be run independent of a web browser. You can still call such scripts from the web, using either PHP's "exec" protocol or directly (in the case of Perl or cgi). Plus, you can cron-job the stuff to check periodically for bounces, mis-sends, or clogged queues.

I built a sophistocated mail processing system for a client using PHP for the front-end administration (web based), MySQL for the message data, and Perl for all the back-end sending and re-sending. It has served them well for several years now, processing thousands of messages per week.

Check out http://search.cpan.o....79/Sendmail.pm if you are interested in going the Perl route.

Cheers,
RP
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1