Mailing List Timeout

the mail() function

Moderators: macek, egami, gesf

Joel
New php-forum User
New php-forum User
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand
Contact:

Mailing List Timeout

Postby Joel » Sat Sep 24, 2005 11:58 pm

I see the "3000" emails post before, but it still doesn't exactly answer my question.

I am making a mailing list, but it is timing out before all the mail is being sent out.

I have put it into a loop to make the email personal with the users name.

One part of the code I am unsure about is use of sleep(). I got this idea from a mailing list tutorial. I scoured the net for these after I found the initial problem and it was meant to stop the script from timing out, which I didn't think would work and sure enough, it didn't.

Any ideas on how I can overcome this? My code is below.

Code: Select all

         //Retrieve newsletter information
         $sql = "SELECT * FROM newsletter WHERE newsletter_id = ".$_GET['send'];
         $result = mysql_query($sql) or die(mysql_error());
         $row = mysql_fetch_assoc($result);
         
         $months = $row['months'];
         
         //Prepare newsletter string filename
         if (strlen($_GET['send']) == 1) {
            $id = "0".$_GET['send'];
         } else {
            $id = $_GET['send'];
         }

         //Read selected newsletter to variable
         $filename = "../newsletter".$id.".htm";
         if ($newsletter = file_get_contents($filename)) {
            $mail = file_get_contents("newsletter.template.htm");
            $mail = str_replace(array("{MONTHS}", "{NEWSLETTER}"), array($months, $newsletter), $mail);
            
            $subject = "DFNZ ".$months." Newsletter";
            
            //Prepare mail list
            $sql = "SELECT * FROM mailinglist";
            $result = mysql_query($sql) or die(mysql_error());
            
            $headers  = "MIME-Version: 1.0\r\n";
            $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
            $headers .= "From: Diamonds Forever NZ <jayne@dfnz.co.nz>\r\n";
            
            $x = 0;
            $hold = 30;
            
            while ($row = mysql_fetch_assoc($result)) {
               $sendmail = str_replace(array("{NAME}", "{EMAIL}", "{CODE}"), array(ucfirst(strtolower($row['name'])), $row['email'], $code), $mail);
               
               if (!mail ($row['email'], $subject, $sendmail, $headers)) {
                  $unsent[] = $row['email'];
               }
               
               $x++;         
               
               if($x == $hold) { // When $x is equal to $hold, a 3 sec delay will occur avoiding php to timeout
                  sleep(3);
                  $x = 0;
               }
            }
            
            echo "<p>The newsletter has been sent to your mailing list.</p>";
            
            if (isset($unsent)) {
               echo "<p><br>However, the following email addresses did not work</p><br>\n";
               foreach ($unsent as $u) {
                  echo "<br>".$u['email']."\n";
               }
            }
            
            $sql = "UPDATE newsletter SET sent = 'True', sent_date = UNIX_TIMESTAMP() WHERE newsletter_id = ".$_GET['send'];
            mysql_query($sql) or die(mysql_error());
            
            
         } else {
            echo "<p>The newsletter selected can not be retrieved so nothing has been sent.</p>";
         }
         

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Postby Alexej Kubarev » Sun Sep 25, 2005 1:33 am

I see you have a problem that this has to be sent to each person with their name.. this is a bit worse as you cant simply use a BCC...

Therefore i suggest increasing script timout time, which has a default of 30 seconds
Then it would also be a good solution making redirect to the next page: something like this:

You count how many emails there are..
lets say 100.
then the links will be like this: send.php?part=1, send.php?part=2 and so on..
so each page will send let's say 10 emails, then redirect you to the next page (next part)
this will help you resolve the problem with script timouts.

I hope you have fixed all the SMTP settings in your php.ini

Joel
New php-forum User
New php-forum User
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand
Contact:

Postby Joel » Sun Sep 25, 2005 1:42 am

Thanks Alexei,

I don't have access to the PHP.INI but my plan was to do 30 at a time or so if there was no quick fix which I wasn't aware of.

I have rewritten my script using PHP Mailer, but the only SMTP I can use are with different ISP's from my hosting company.

I also can't use set_time_limit as the server is running in Safe Mode.

Looks like I will be doing a divided up mailing system.

User avatar
gesf
Moderator
Moderator
Posts: 1717
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden
Contact:

Postby gesf » Mon Sep 26, 2005 1:02 am

Hey Joel, you can talk with your hoster to make that changes for you also to disable safe_mode.

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Postby Alexej Kubarev » Mon Sep 26, 2005 12:39 pm

well... safe_mode should not be disabled on hostings..however you may want to talk to them regarding your problem and how they would recommend you to solve it..


Return to “PHP coding => Mail”

Who is online

Users browsing this forum: No registered users and 1 guest