Results 1 to 3 of 3

Thread: Does this need a CRON or something else?

  1. #1
    Join Date
    Feb 2007
    Location
    New York
    Posts
    12

    Default Does this need a CRON or something else?

    I have a process to batch the sending of mail messages.

    I use a form to input selection criteria, and other variable information, and on SUBMIT, that form calls another php file that does the mySQL query, walk through the ResultSet, sends each mail message, and aggregates success metrics which are sent at the end of the process. Also, the results are echoed to screen so the user has confirmation of execution...

    To accommodate the 500 message per hour limit, I added a "sleep(8)" statement after each successful "mail()" statement.

    When running this process in real time, the user's PC is "frozen" while it waits for the execution to complete, and with several hundred messages, the PC is locked for an hour or more (see code below)...

    I think I may need a CRON or some other execution file that would enable a simple message to the screen ("Your job is being run..."), and release the user's PC.

    I would be most appreciative of any advice on how to resolve this (I have no experience with CRON).

    Thanks in advance!
    JTG


    Code:
    //--- MAILER form...  this is a form that
    //--- gets input from screen for criteria for this job
    //--- On SUBMIT, call Mail_Batcher_Send.php file to query DB, 
    //---     select recipients, and send each mail message
    //--- Here is FORM tag that invokes Mailer scripts on SUBMIT:
    
    <form name="Mailer" method="post" action="Scripts/Mail_Batcher_Send.php">
    
    ########[ next form ]#############
    
    //--- SEND_MAIL form (Mail_Batcher_Send.php).  Invoked when previous file is submitted
    //--- This will query database to select a batch of records (usually 300 to 600 in resul tset)
    //--- Mail message is coonstructed with data from result set (code omitted here for brevity)
    //--- and sends each mail message ( $ResultMsg and $ErrorTrap variables retain success or failure stats
    //--- and send back to originator to report results of process:
    
    <?php
    
    $ErrorTrap = "";
    $ResultMsg = "";
    $Counter = 0 ;
    $Failed = 0 ;
    
    // Make a MySQL Connection
    mysql_connect($hostname,$username,$password) or die(mysql_error());
    @mysql_select_db($database) or die( "Unable to select database" . mysql_error());
    
    $newQuery = "SELECT * FROM Recipients ORDER BY $sortBy";
    $result = mysql_query($newQuery) or die(mysql_error());
    while($row = mysql_fetch_array($result)){
    	$recipientAddress = $row['EmailAddress'];
    	$RecipientName = $row['LastName'] . ", " . $row['FirstName'];
    	$GreetingName = $row['GreetingName'];
    	$PersMsg = $row['PersMsg'];
    
    	if (mail($recipientAddress,$Subject,$msg,$headers)) {
    		$ResultMsg .= "<tr><td nowrap>Sent to: </td><td nowrap>" . $RecipientName . "</td><td nowrap>" . $recipientAddress . "</td></tr>" . $eol;
    		$Counter = $Counter + 1;
    		sleep(8);  //-- Pause to remain below the 500 message per hour limit
    	} else {
    		$ErrorTrap .= "<tr><td nowrap>NOT Sent: </td><td nowrap>" . $RecipientName . "</td><td nowrap>" . $recipientAddress . "</td></tr>" . $eol;
    		$Failed = $Failed + 1 ;
    	}
    }
    
    $ResultMsg .= $eol . $eol . $eol . "<tr><td colspan=3 bgcolor=navy height=5></td></tr>" . $eol . $ErrorTrap . $eol;
    $Summary = $msgHdr . $Counter . " sent<br>" . $Failed . " failed...<br><table border=0 cellspacing=0 cellpadding=0><tr><td colspan=3 bgcolor=navy height=5></td></tr><tr><td><b>Action</b></td><td><b>NAME</b></td><td><b>eMAIL</b></td></tr>" . $ResultMsg . $eol . "</table>" . $eol;
    
    //--- Send ResultMsg
    	mail($email6,$Subject2,$Summary,$headers);
    
    
    //--- Echo results to screen so user knows process executed
    	echo "<b><u>Mailer Results Summary</u></b><br>" . $eol . "Sent by: " . $FullName . "....Sent From: " . $SentFrom . "<br><br>" . $eol . $Counter . " sent<br>" . $eol . $Failed . " failed...<br>" . $eol . "--------------------<br><b><u>Failures</u></b><br><table border=0 cellspacing=0 cellpadding=0>" . $eol . $ErrorTrap . $eol . "</table>" . $eol;
    
    ?>

  2. #2
    Join Date
    Nov 2006
    Location
    Sydney, Australia
    Posts
    4,951

    Default

    The way I handle bulk mail sends is to have the send job write all the info on the emails to be sent to a couple of tables in a database - one table contains the message to be sent, the other contains a list of email addresses, a link to the message, and a flag as to whether it has been sent yet or not.

    I then have a cron job set to run at regular intervals that will extract the first so many requests from the database and send them flagging them as sent. For example I have one set to send 80 emails which runs every 15 minutes throughout the day each month when I send that particular email out. So the first run sends the first 80 emails, fifteen minutes later the next 80 are sent and so on until they are all sent. While the script still continues to run every 15 minutes for the rest of that day with no emails outstanding it basically just checks that and stops.

  3. #3
    Join Date
    Feb 2007
    Location
    New York
    Posts
    12

    Default Your solution sounds good...

    I have a few follow-up questions:
    1. Your "Message" database then contains the full code for each message, so the personalization is not lost (is that correct?) -- If so, can you include in each message a unique link to a web page that populates the associated message for use by recipients that do not use an HTML eMail client (so they can read the message on line)? (I suspect a simple query page would be used to accomplish this)
    2. Do you purge your message DB after a specified time period, or do you retain it as a "history" file?
    3. Where can I find "How to" guidance for creating and setting-up CRON jobs?

    I appreciate your inputs -- I am fairly experienced with writing code for client-server environments, but I am a relative newbie in the php/mySQL world.

    Thanks!

Tags for this Thread

Posting Permissions

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