SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict Philip Toews's Avatar
    Join Date
    Dec 2001
    Location
    Kuala Belait, Brunei
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Reloading a page from within PHP

    I've been banging my head against the wall for several days now trying to get my newsletter mailing script to send out newsletters in lots of 50. I want the script to grab the subscribers list, send out 50 newsletters then reload the page tracking the variables for the script and then moving on to the next 50 and so on.

    I have included the script...I'm sorry it is long...I shortened it considerably in this effort to get help. Can ANYONE please offer a solution ? The javascript solution just doesn't work. The page does not reload and the script stops after processing the first group as defined in the $perpage variable.

    I have tried to get help in another thread...but no answers have surfaced yet...

    that thread is here

    Here is my code...again...sorry it is long:

    PHP Code:
     <?php include("accesscontrol.php"); ?>
    <?php 
    include("admin_accesscontrol.php"); ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

    <html><head><title>SEND NEWSLETTER</title></head>
    <body>
    <?php

    //connect to the db
    $cnx mysql_connect($dbhost$dbuser$dbpass);
    mysql_select_db('teachingfish',$cnx);

    //get all the newsletter data
    $newsletter=@mysql_query("SELECT articles_headline, articles FROM newsletter_management WHERE ID=$id");
    $newsletter mysql_fetch_array($newsletter);
    $articles_headline $newsletter["articles_headline"];
    $articles $newsletter["articles"];

    //build the html version of the newsletter...the actual newsletter is MUCH longer
    $htmlmessage "
    <html><header><link rel='stylesheet' href='http://www.teachingfish.com/styles.css' type='text/css'></header>
    <body bgcolor='#B0C0D0'>
    <p>$
    $articles_headline</p>
    <p>
    $articles</p>
    </body></html>
    "
    ;

    //build the text version of the newsletter...much longer as well
    $textmessage="

    *** 
    $articles_headline ***

    $articles

    "


    $textmessage preg_replace("#\<a href=(('([^']*)')|(\"([^\"]*)\"))>(.*)\</a>#isU""\\3\\5"$textmessage);

    //set the subject and header for the newsletter e-mail
    $subject="teachingfish.com newsletter Issue # $issue"
    $textheader "From:newsletter@teachingfish.com";
    $htmlheader "From:newsletter@teachingfish.com
    MIME-Version: 1.0 
    Content-type: text/html; charset=iso-8859-1"
    ;


    ini_set('max_execution_time''300'); // sets timeout to 5 minutes

    $perpage 2// number of users to e-mail every time
    if (!is_int($startnum)) {
        
    $startnum 0;
    }

    echo 
    '<p>Emailing ' $perpage ' users, starting at record ' $startnum+.'...</p>';

    $subscribers mysql_query("SELECT email, newsletter_format FROM newsletter WHERE validation = 1 LIMIT $startnum,$perpage");
    if (!
    $subscribers) {
      echo(
    "<p>Error retrieving subscriber list from database!<br />".
           
    "Error: " mysql_error() . "</p>");
      exit();
    }
    while (
    $subscriber mysql_fetch_array($subscribers)) {
        
    $to htmlspecialchars($subscriber['email']);
        
    $newsletter_format htmlspecialchars($subscriber['newsletter_format']);
        if (
    $newsletter_format == 'html') {
            
    mail($to$subject$htmlmessage$htmlheader);
            
    $htmlsent++;
        } elseif (
    $newsletter_format == 'text') {
            
    mail($to$subject$textmessage$textheader);
            
    $textsent++;
        }
    }

    echo 
    '<h2>Done! Taking you to next page, please hold...</h2>';
    echo 
    "\n<script language=\"javascript\">\n";
    echo 
    "<!--\n";
    echo 
    "window.location=\"" $PHP_SELF "?startnum=" $startnum+$perpage "\";\n";
    echo 
    "// -->\n";
    echo 
    "</script>\n";
    echo (
    "<h1>Newsletter # $issue Sent</h1>");
    echo (
    "<h3>Newsletter # $issue sent to $htmlsent html subscribers and $textsent text subscribers</h3>");
    echo (
    "<h3>A total of $sent_to subscribers</h3>");
    ?>


    </body>
    </html>
    Please help....
    Philip Toews Professional esl Educator and ASP.NET wannabe

    http://www.philiptoews.com
    philip@philiptoews.com

  2. #2
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm I think it is because your theory is a bit wrong as you are not actually putting the mail sending into a loop.

    AFAICT you should do something like this:

    • Define number of people to send to
    • Get all people from DB
    • Count number of people in db
    • Send mail in a loop based on number of people and a starting point
    • Increment startpoint
    • Continue loop til all people sent to
    • End Script


    So something like:

    [php]

    $perpage = 2; // number of users to e-mail every time
    if (!is_int($startnum)) {
    $startnum = 0;
    }

    echo '<p>Emailing ' . $perpage . ' users, starting at record ' . $startnum+1 .'...</p>';

    $subscribers = mysql_query("SELECT email, newsletter_format FROM newsletter WHERE validation = 1 LIMIT $startnum,$perpage");
    if (!$subscribers) {
    echo("<p>Error retrieving subscriber list from database!<br />".
    "Error: " . mysql_error() . "</p>");
    exit();

    }

    $numbersentto=0;
    $numberpeople=mysql_num_rows($subscribers);

    while($numberpeople!=$numbersento) {

    while ($subscriber = mysql_fetch_array($subscribers)) {
    $to = htmlspecialchars($subscriber['email']);
    $newsletter_format = htmlspecialchars($subscriber['newsletter_format']);
    if ($newsletter_format == 'html') {
    mail($to, $subject, $htmlmessage, $htmlheader);
    $htmlsent++;
    } elseif ($newsletter_format == 'text') {
    mail($to, $subject, $textmessage, $textheader);
    $textsent++;
    }
    }
    $numberpeople++;
    }
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  3. #3
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm I think it is because your theory is a bit wrong as you are not actually putting the mail sending into a loop.

    AFAICT you should do something like this:

    • Define number of people to send to
    • Get all people from DB
    • Count number of people in db
    • Send mail in a loop based on number of people and a starting point
    • Increment startpoint
    • Continue loop til all people sent to
    • End Script


    So something like:

    PHP Code:

    $perpage 
    2// number of users to e-mail every time
    if (!is_int($startnum)) {
        
    $startnum 0;
    }

    echo 
    '<p>Emailing ' $perpage ' users, starting at record ' $startnum+.'...</p>';

    $subscribers mysql_query("SELECT email, newsletter_format FROM newsletter WHERE validation = 1 LIMIT $startnum,$perpage");
    if (!
    $subscribers) {
      echo(
    "<p>Error retrieving subscriber list from database!<br />".
           
    "Error: " mysql_error() . "</p>");
      exit();

    }

    $numbersentto=0;
    $numberpeople=mysql_num_rows($subscribers);

    while(
    $numberpeople!=$numbersento) {

    while (
    $subscriber mysql_fetch_array($subscribers)) {
        
    $to htmlspecialchars($subscriber['email']);
        
    $newsletter_format htmlspecialchars($subscriber['newsletter_format']);
        if (
    $newsletter_format == 'html') {
            
    mail($to$subject$htmlmessage$htmlheader);
            
    $htmlsent++;
        } elseif (
    $newsletter_format == 'text') {
            
    mail($to$subject$textmessage$textheader);
            
    $textsent++;
        }
    }
    $numbersentto++;
    $numberpeople++;

    That is totally off the top of my head, so you might get a parse error if I have put { } in the wrong place etc, but that should be the general idea...
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  4. #4
    SitePoint Addict Philip Toews's Avatar
    Join Date
    Dec 2001
    Location
    Kuala Belait, Brunei
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    script never ends

    I tried your solution and I counted the braces (which were correct). I ripped it and I got a never ending script...it never lands on the last part of the script:


    The error message that I get is:

    CGI Timeout
    The specified CGI application exceeded the allowed time for processing. The server has deleted the process.
    Could this problem be related to the fact that I am running the PHP cgi on my local box?


    Note that the javascript portion is slightly different now...
    PHP Code:
    <script language="javascript">
    <!--
    window.location='"+<?= $PHP_SELF?>+ "?startnum="+ <?= $startnum+$perpage?>+"'";
    // -->
    </script>
    <?php
    echo ("<h1>Newsletter # $issue Sent</h1>");
    ?>
    <p><a href="admin.php"><img src='images/main-console.gif' width='114' height='51' border='0' alt='Go back to the main console'></a><a href="newnewsletter.php"><img src='images/new-newsletter.gif' width='114' height='51' border='0' alt='Create a new newsletter'></a><a href="newsletters.php"><img src='images/show-newsletters.gif' width='114' height='51' border='0' alt='Go back to the newsletter management console'></a><br><br></p>
    <?php

    $sent_to 
    = ($htmlsent $textsent);
    $sql "UPDATE newsletter_management SET sent_to='$sent_to', send_date = curdate(), sent='yes' WHERE ID=$id";
    $newslettersent = @mysql_query($sql);
    if (!
    $newslettersent) {
      echo(
    "<p>Error updating newsletter_management table!<br />".
           
    "Error: " mysql_error() . "</p>");
      exit();
    }
    echo (
    "<h3>Newsletter # $issue sent to $htmlsent html subscribers and $textsent text subscribers</h3>");
    echo (
    "<h3>A total of $sent_to subscribers</h3>");


    ?>
    Philip Toews Professional esl Educator and ASP.NET wannabe

    http://www.philiptoews.com
    philip@philiptoews.com

  5. #5
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The way I described doesn't need the Javascript anymore. I haven't got time now but will look at it later.
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  6. #6
    SitePoint Addict Philip Toews's Avatar
    Join Date
    Dec 2001
    Location
    Kuala Belait, Brunei
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks...I'll wait for your reply

    Thanks weirdbeard

    I'll wait for your response and take a look at it tomorrow. And I WILL drink beer with you...hell I'll buy...that is if your ever in my neck of the woods!


    BTW how can I be sure that ALL the mails get sent and how can I be sure that I don't get timout errors?

    p
    Last edited by Philip Toews; Mar 4, 2002 at 08:02.
    Philip Toews Professional esl Educator and ASP.NET wannabe

    http://www.philiptoews.com
    philip@philiptoews.com

  7. #7
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That error means that PHP had to stop the script from running because it had gone past the maximum execution time. You can extend the time with set_time_limit() or you can alter the php.ini

    http://www.php.net/manual/en/functio...time-limit.php

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  8. #8
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool you won't be needing my help in javascript anymore. ...I was gonna work out just about the same thing in the next bit but since weirdy has it
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  9. #9
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK I just noticed the major error in the code, don't increment the numberpeople each time (just the number of people it has been sent to!). So remove numberpeople++ from the loop.
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  10. #10
    SitePoint Addict Philip Toews's Avatar
    Join Date
    Dec 2001
    Location
    Kuala Belait, Brunei
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OK...but how can I be sure ALL the mails go out

    I can set the max execution time:

    PHP Code:
    ini_set('max_execution_time''0'); // never timeout 
    but that just means the script never dies.... could that cause problems when mailing out to thousands of subscribers? I anticipate thousands...I don't want to have problems. I only have a modem connection to the net. Broadband doesn't exist where I live. If I have a net connection problem while the script is running then I will never know how many mails went out because it updates the db AFTER the script runs.

    Any suggestions here? The irony is that now that I have adjusted the script it is back to what I originally wrote. Also...I had to remove weirdy's nested loop solution and went back to a simple while loop that just goes through the array results of the first db query. Now it works...but I'M BACK TO WHERE I STARTED! Just for reference...here's the code that works...but how do I make sure that ALL the mails get sent?




    Also, there seems little point to having the "$perpage" variable set. I may as well cut out the portions of the script that reference that variable. This goes back to the original problem. I want to break the mailouts up into blocks so that I can avoid timeout issues and connection failure problems. How does this new version of the script address those issues? The attempt to get javascript to reload the page was to make the page reload after $perpage number of mails were sent and then process the next block. I don't understand how this script does this. I'm a newbie so maybe I am missing something. The processing of the entire mailing list is the only option with this script now...so it seems I am back where I started. Am I missing something here?

    Help!



    PHP Code:

    <?php
    ini_set
    ('max_execution_time''0'); // sets timeout to never

    $subscribers mysql_query("SELECT email, newsletter_format FROM newsletter WHERE validation = 1");
    if (!
    $subscribers) {
      echo(
    "<p>Error retrieving subscriber list from database!<br />".
           
    "Error: " mysql_error() . "</p>");
      exit();
    }

    while (
    $subscriber mysql_fetch_array($subscribers)) {
        
    $to htmlspecialchars($subscriber['email']);
        
    $newsletter_format htmlspecialchars($subscriber['newsletter_format']);
        if (
    $newsletter_format == 'html') {
            
    mail($to$subject$htmlmessage$htmlheader);
            
    $htmlsent++;
        } elseif (
    $newsletter_format == 'text') {
            
    mail($to$subject$textmessage$textheader);
            
    $textsent++;
        }
    }


    //mails sent...update the newsletters table
    echo ("<h1>Newsletter # $issue Sent</h1>");
    ?>

    <?php

    $sent_to 
    = ($htmlsent $textsent);
    $sql "UPDATE newsletter_management SET sent_to='$sent_to', send_date = curdate(), sent='yes' WHERE ID=$id";
    $newslettersent = @mysql_query($sql);
    if (!
    $newslettersent) {
      echo(
    "<p>Error updating newsletter_management table!<br />".
           
    "Error: " mysql_error() . "</p>");
      exit();
    }
    echo (
    "<h3>Newsletter # $issue sent to $htmlsent html subscribers and $textsent text subscribers</h3>");
    echo (
    "<h3>A total of $sent_to subscribers</h3>");


    ?>
    p
    Last edited by Philip Toews; Mar 6, 2002 at 01:28.
    Philip Toews Professional esl Educator and ASP.NET wannabe

    http://www.philiptoews.com
    philip@philiptoews.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •