[pmwiki-devel] [pmwiki-users] Newsletter throttling

The Editor editor at fast.st
Mon Oct 30 12:55:32 CST 2006


On 10/30/06, Patrick R. Michaud <pmichaud at pobox.com> wrote:
> > [...]  But would that slow down the
> > site?  Would the page halt the loading until all 100 emails were sent?
> > That could be slow.  Or would it work independently.  I guess I don't
> > understand this process very well.
>
> Sending out 100 emails could take some amount of time, especially
> if the SMTP server isn't local.  But the best thing to do is to
> tell PmWiki to do any background work only *after* it's completed
> the request (e.g., via register_shutdown_function).  This is what
> the notify.php script does for sending out its email messages.

I sent this once before but it may have got missed in the switch to
the new developers maillist.  Can you explain how to do this using the
register_shutdown_function as opposed to this bits of  "ob_*" code I
borrowed from some tutorial I don't really understand.

Also, my current strategy is to just start a php process when the
newsletter is sent that runs invisibly in the background till all the
emails are sent, throttling it by the sleep function, and returning an
immediate, simple message to the screen that messages have been fed to
a queue so the admin can continue clicking around.

By the way, the code was modified from something evidently used over
at dokuwiki.

Cheers,
Caveman

<?php

## This function is sent all needed email parameters from a zap form
containing: emaillist, emailfrom, emailsubject, emailbody.  When
called from emailer function, ZAP does all error checking, including
checks to see if $ZAPnews is set, if list exists, etc.  Can get optional
alerts every x emails (that things are moving along) and different
rates of throttling (sleeping y seconds between emails)--from the
zapform.

SDV($ZAPnews, "true"); // used in ZAP emailer to check if zap news available
SDV($ZAPlistgroup, "MailList"); // default group for all pmwiki MailLists

function ZAPnews($list, $from, $subject, $message, $alerts=0, $throttle=20) {
    global $ZAPlistgroup, $ZAPadmin;

//This part is supposed to return to the page without closing the php thread
    ob_implicit_flush(TRUE);
    @ignore_user_abort(true);
    sendGIF();
    ob_implicit_flush(FALSE);
    ob_start();

//This is the part I'll be using to send the emails.
    $c = 0;
    if(PageExists("$ZAPlistgroup.$list")) {
         $page = ReadPage($xx);
         $contents = substr($page['text'], 3, -3);
         $r = explode("\n", $contents);
         foreach ($r as $recipient) {
              $c = $c + 1;
              if ($c == 1) $begin = substr($r, 0, strpos($r, "@")) . "@*****";
              $mergedbody = str_replace("{email}", $to, $body);
              mail($r, $subject, $mergedbody, "From: $from");
              if (($c == $alerts) && (isset($ZAPadmin[$list]))) {
                   $end = substr($r, 0, strpos($r, "@")) . "@*****";
                   $alertsubject = "ZAPnews alert";
                   $alerttime = strftime(%c, time());
                   $alertbody = "$subject sent, from $begin to $end
by $alerttime";
                   mail($ZAPadmin[$list], $alertsubject, $mergedbody,
"From: $from");
                   $c = 0;
                   }
              sleep($throttle);
              }

//used to close off the ob_start, whatever that is
    ob_end_clean()
    }


//the function used above

function sendGIF() {
   $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7');
   header('Content-Type: image/gif');
   header('Content-Length: '.strlen($img));
   header('Connection: Close');
   print $img;
}



More information about the pmwiki-devel mailing list