Re: [PHP] limiting the amount of emails sent at a time in a batch send



I would use a temporary table to hold all the email created in one job.

Have a status field marked as sent, waiting or open.

Have a cron job set 'x' number of open emails to waiting. Execute periodically.
Have a cron job to send all waiting email and update status to sent. Execute periodically.

Stephen

Richard Kurth wrote:
I want to limit these script two send 100 email and then pause for a few seconds and then send another 100 emails and repeat this tell it has sent all the emails that are dated for today. This script is runs by cron so it is running in the background.

How would I do this and is it the best way to do it. I am using swift mailer to send the mail.

I think I would use limit 100 in the query but how would I tell it to get the next 100.

$today = date("Y-m-d");
# Cache the Contact that will recive campain messages today
$query = "SELECT emailmessages. * , contacts.* FROM emailmessages, contacts WHERE emailmessages.contact_id = contacts.id AND emailmessages.nextmessagedate = '$today' AND contacts.emailstatus = 'Active'"; $DB_Contact_Result = mysql_query($query);
if (!$DB_Contact_Result) {
die('Invalid query: ' . mysql_error());
}
while ($this_row = mysql_fetch_array ($DB_Contact_Result)){

$CustomerID=$this_row["id"];

//get the message for the contact
$query1 = "SELECT * FROM emailcampaign where member_id = '$this_row[members_id]' AND campaign_id = '$this_row[emailcampaign]' AND day = '$this_row[email_number]'";

$DB_Mail_Result = mysql_query($query1);
if (!$DB_Mail_Result) {
die('Invalid query: ' . mysql_error());
}
$Mail_row = mysql_fetch_array($DB_Mail_Result);
//get member stuff
$query2 = "SELECT * FROM member where members_id = '$this_row[members_id]'";

$DB_Member_Result = mysql_query($query2);
if (!$DB_Member_Result) {
die('Invalid query: ' . mysql_error());
}

$subject=stripslashes($Mail_row['subject']);

$message=stripslashes($Mail_row['textmessage']);

$hmessage=stripslashes($Mail_row['htmlmessage']);

$swiftPlain = $message;
$MailFrom1=$this_row['emailaddress'];
$MailFromName1=$this_row['firstname'];

$full_name=stripslashes($this_row['firstname']) ." " . stripslashes($this_row['lastname']);
//Create the message
$message = new Swift_Message($subject);
$message->attach(new Swift_Message_Part($swiftPlain));
$message->attach(new Swift_Message_Part($swiftHtml, "text/html"));
$message->setReturnPath($MailAddReplyTo);
$message->setReplyTo($MailFrom1,$MailFromName1);
//Now check if Swift actually sends it
$swift->send($message, $this_row['emailaddress'],$MailFrom1,$MailFromName1);

#After we send the email we need to update the database to send the next message
# get the next message number
$nextday=getnextday(stripslashes($this_row['emailcampaign']),stripslashes($this_row['members_id']),stripslashes($this_row['email_number']));

# get the new dates
$newdate=getnewdate(stripslashes($this_row['emailstarted']),$nextday);
#update the emailmessages
//unsubscribecode
updateemailmessages($unpass,$nextday,$newdate,stripslashes($this_row['em_id']),stripslashes($this_row['email_number']));




}//end of mail loop



.



Relevant Pages

  • Re: Serial - USB
    ... John Tytgat wrote: ... podule and had exactly the same problem. ... just doing one query and waiting for a reply sometimes worked or at ...
    (comp.sys.acorn.misc)
  • Re: general problems with queries
    ... Creating a Query will not give you a form, so you can quit waiting... ... If you want the query to reference a Form for its criteria, ... they are bound (e.g. txtPrefix as a Textbox bound to the field ...
    (microsoft.public.access.queries)
  • Re: Query Wait Option
    ... > How can I determine what resources my query is waiting for? ... > If the query is waiting on memory how can I determine this? ... > is possible for an undetectable deadlock to occur. ...
    (microsoft.public.sqlserver.server)
  • Query Wait Option
    ... How can I determine what resources my query is waiting for? ... If the query is waiting on memory how can I determine this? ... is possible for an undetectable deadlock to occur. ...
    (microsoft.public.sqlserver.server)
  • Re: Transfer a lot of records from Database
    ... I want to do a query to a remote DB (MySQL) which is expect to return a ... cron job during mid-night. ...
    (comp.lang.java.programmer)