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

2008-07-31 Thread brian

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.


There's no need to limit the DB query, nor to track what's been sent by 
updating the DB. Grab all of the addresses at once and let SwiftMailer 
deal with the throttling:


require('Swift/lib/Swift.php');
require('Swift/lib/Swift/Connection/SMTP.php');

/* this handles the throttling
 */
require('Swift/lib/Swift/Plugin/AntiFlood.php');


/* this holds all of your addresses
 */
$recipients = new Swift_RecipientList();


/* Grab the addresses from the DB (this is using MDB2)
 */
$result = ...

while ($row = $result-fetchRow())
{
$recipients-addTo($row['address'], $row['name']);
}
@$result-free();

try
{
$swift = new Swift(new Swift_Connection_SMTP('localhost'), 
'your_domain');

set_time_limit(0);
$swift-log-enable();

/* 100 mails per batch with a 60 second pause between batches
 */
$swift-attachPlugin(new Swift_Plugin_AntiFlood(100, 60), 'anti-flood');

flush();

$message = new Swift_Message('your subject');

$message-setCharset('utf-8');
$message-setReplyTo(...);
$message-setReturnPath(...);
$message-headers-set('Errors-To', ...);

$message-attach(new Swift_Message_Part($plain_content));
$message-attach(new Swift_Message_Part($html_content, 'text/html'));

	$num_sent = $swift-batchSend($message, $recipients, new 
Swift_Address(..., '...'));


$swift-disconnect();

...


This is a rough example taken from my own script. This would need to be 
modified if you're not sending the same message to all recipients, of 
course. It's not clear to me from your example.


b

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



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

2008-07-31 Thread Andrew Ballard
On Thu, Jul 31, 2008 at 1:27 PM, brian [EMAIL PROTECTED] wrote:
 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.

 There's no need to limit the DB query, nor to track what's been sent by
 updating the DB. Grab all of the addresses at once and let SwiftMailer deal
 with the throttling:

 require('Swift/lib/Swift.php');
 require('Swift/lib/Swift/Connection/SMTP.php');

 /* this handles the throttling
  */
 require('Swift/lib/Swift/Plugin/AntiFlood.php');


 /* this holds all of your addresses
  */
 $recipients = new Swift_RecipientList();


 /* Grab the addresses from the DB (this is using MDB2)
  */
 $result = ...

 while ($row = $result-fetchRow())
 {
$recipients-addTo($row['address'], $row['name']);
 }
 @$result-free();

 try
 {
$swift = new Swift(new Swift_Connection_SMTP('localhost'),
 'your_domain');

set_time_limit(0);
$swift-log-enable();

/* 100 mails per batch with a 60 second pause between batches
 */
$swift-attachPlugin(new Swift_Plugin_AntiFlood(100, 60),
 'anti-flood');

flush();

$message = new Swift_Message('your subject');

$message-setCharset('utf-8');
$message-setReplyTo(...);
$message-setReturnPath(...);
$message-headers-set('Errors-To', ...);

$message-attach(new Swift_Message_Part($plain_content));
$message-attach(new Swift_Message_Part($html_content, 'text/html'));

$num_sent = $swift-batchSend($message, $recipients, new
 Swift_Address(..., '...'));

$swift-disconnect();

 ...


 This is a rough example taken from my own script. This would need to be
 modified if you're not sending the same message to all recipients, of
 course. It's not clear to me from your example.

 b


Nice! I'll have to look into this library some time. How do you
control it to prevent sending the same message though? I can't imagine
this is called from a web page, because I'm guessing it would take a
few minutes to finish. If it's called from a cron job, don't you still
have to somehow flag the message as having been delivered so that the
next process doesn't come along and send the same thing all over
again?

Andrew

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



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

2008-07-31 Thread brian

Andrew Ballard wrote:

On Thu, Jul 31, 2008 at 1:27 PM, brian [EMAIL PROTECTED] wrote:

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.

There's no need to limit the DB query, nor to track what's been sent by
updating the DB. Grab all of the addresses at once and let SwiftMailer deal
with the throttling:

require('Swift/lib/Swift.php');
require('Swift/lib/Swift/Connection/SMTP.php');

/* this handles the throttling
 */
require('Swift/lib/Swift/Plugin/AntiFlood.php');


/* this holds all of your addresses
 */
$recipients = new Swift_RecipientList();


/* Grab the addresses from the DB (this is using MDB2)
 */
$result = ...

while ($row = $result-fetchRow())
{
   $recipients-addTo($row['address'], $row['name']);
}
@$result-free();

try
{
   $swift = new Swift(new Swift_Connection_SMTP('localhost'),
'your_domain');

   set_time_limit(0);
   $swift-log-enable();

   /* 100 mails per batch with a 60 second pause between batches
*/
   $swift-attachPlugin(new Swift_Plugin_AntiFlood(100, 60),
'anti-flood');

   flush();

   $message = new Swift_Message('your subject');

   $message-setCharset('utf-8');
   $message-setReplyTo(...);
   $message-setReturnPath(...);
   $message-headers-set('Errors-To', ...);

   $message-attach(new Swift_Message_Part($plain_content));
   $message-attach(new Swift_Message_Part($html_content, 'text/html'));

   $num_sent = $swift-batchSend($message, $recipients, new
Swift_Address(..., '...'));

   $swift-disconnect();

...


This is a rough example taken from my own script. This would need to be
modified if you're not sending the same message to all recipients, of
course. It's not clear to me from your example.

b



Nice! I'll have to look into this library some time. How do you
control it to prevent sending the same message though?


I'm not sure about that. Mine is for a newsletter (not personalised) so 
Swift just needs a list of addresses. I can't remember if AntiFlood can 
be used for many unique mails. I suspect that it doesn't.



I can't imagine
this is called from a web page, because I'm guessing it would take a
few minutes to finish.


cron. But, if you wanted it fired from an admin page (I can't imagine a 
good situation where something like this would be public) you could add 
ignore_user_abort() and then jazz up the page with some async JS.


If it's called from a cron job, don't you still

have to somehow flag the message as having been delivered so that the
next process doesn't come along and send the same thing all over
again?


Yeah, I just twigged to the fact that your messages are stored in the 
database. This is some kind of messaging system? As opposed to a 
newsletter-type thing, I mean. As above, I don't know that AntiFlood  is 
what you want. That's meant for multiple-recipient mails.


There's also a Throttler plugin, though, again, it's meant for batches.

You might also look at the IMAP functions. Instead of storing the 
messages in the DB, you can let the MTA deal with pushing them out and 
cut Swift (and the cro0n job) out of the loop.


b

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



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

2008-07-31 Thread Andrew Ballard
On Thu, Jul 31, 2008 at 4:24 PM, brian [EMAIL PROTECTED] wrote:
 Andrew Ballard wrote:

 On Thu, Jul 31, 2008 at 1:27 PM, brian [EMAIL PROTECTED] wrote:

 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.

 There's no need to limit the DB query, nor to track what's been sent by
 updating the DB. Grab all of the addresses at once and let SwiftMailer
 deal
 with the throttling:

 require('Swift/lib/Swift.php');
 require('Swift/lib/Swift/Connection/SMTP.php');

 /* this handles the throttling
  */
 require('Swift/lib/Swift/Plugin/AntiFlood.php');


 /* this holds all of your addresses
  */
 $recipients = new Swift_RecipientList();


 /* Grab the addresses from the DB (this is using MDB2)
  */
 $result = ...

 while ($row = $result-fetchRow())
 {
   $recipients-addTo($row['address'], $row['name']);
 }
 @$result-free();

 try
 {
   $swift = new Swift(new Swift_Connection_SMTP('localhost'),
 'your_domain');

   set_time_limit(0);
   $swift-log-enable();

   /* 100 mails per batch with a 60 second pause between batches
*/
   $swift-attachPlugin(new Swift_Plugin_AntiFlood(100, 60),
 'anti-flood');

   flush();

   $message = new Swift_Message('your subject');

   $message-setCharset('utf-8');
   $message-setReplyTo(...);
   $message-setReturnPath(...);
   $message-headers-set('Errors-To', ...);

   $message-attach(new Swift_Message_Part($plain_content));
   $message-attach(new Swift_Message_Part($html_content,
 'text/html'));

   $num_sent = $swift-batchSend($message, $recipients, new
 Swift_Address(..., '...'));

   $swift-disconnect();

 ...


 This is a rough example taken from my own script. This would need to be
 modified if you're not sending the same message to all recipients, of
 course. It's not clear to me from your example.

 b


 Nice! I'll have to look into this library some time. How do you
 control it to prevent sending the same message though?

 I'm not sure about that. Mine is for a newsletter (not personalised) so
 Swift just needs a list of addresses. I can't remember if AntiFlood can be
 used for many unique mails. I suspect that it doesn't.

 I can't imagine
 this is called from a web page, because I'm guessing it would take a
 few minutes to finish.

 cron. But, if you wanted it fired from an admin page (I can't imagine a good
 situation where something like this would be public) you could add
 ignore_user_abort() and then jazz up the page with some async JS.

 If it's called from a cron job, don't you still

 have to somehow flag the message as having been delivered so that the
 next process doesn't come along and send the same thing all over
 again?

 Yeah, I just twigged to the fact that your messages are stored in the
 database. This is some kind of messaging system? As opposed to a
 newsletter-type thing, I mean. As above, I don't know that AntiFlood  is
 what you want. That's meant for multiple-recipient mails.

 There's also a Throttler plugin, though, again, it's meant for batches.

 You might also look at the IMAP functions. Instead of storing the messages
 in the DB, you can let the MTA deal with pushing them out and cut Swift (and
 the cro0n job) out of the loop.

 b


The case I have in mind is for a newsletter, and in this instance they
are all the same. In this instance, I am limited by the host to 2500
messages/hour. (Originally it was 900). At this point, 2500 is more
than enough for this group, but I'd still like to play kindly with the
server resources since this is a shared host. I just set up a generic
message queue and dump mail there rather than sending it real time.
For now, all the messages in a given batch are the same, but I was
leaving flexibility in case I hit a situation where that changed.

It looks like this would work pretty well when I'm up for a rewrite. :)

I understand the general idea:

1. read message body from storage (file, database, etc.)
2. get result set containing addresses from database
3. build recipient list from result set
4. send everything to sendBatch()

It seems to me that step 5 still has to either (a) delete the message
body from storage or else (b) flag it as sent so that the next process
spawned via cron sees that there is no work to do and dies rather than
repeating the whole process again.

Andrew

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



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

2008-07-31 Thread Richard Kurth

Andrew Ballard wrote:

On Thu, Jul 31, 2008 at 4:24 PM, brian [EMAIL PROTECTED] wrote:
  

Andrew Ballard wrote:


On Thu, Jul 31, 2008 at 1:27 PM, brian [EMAIL PROTECTED] wrote:
  

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.
  

There's no need to limit the DB query, nor to track what's been sent by
updating the DB. Grab all of the addresses at once and let SwiftMailer
deal
with the throttling:

require('Swift/lib/Swift.php');
require('Swift/lib/Swift/Connection/SMTP.php');

/* this handles the throttling
 */
require('Swift/lib/Swift/Plugin/AntiFlood.php');


/* this holds all of your addresses
 */
$recipients = new Swift_RecipientList();


/* Grab the addresses from the DB (this is using MDB2)
 */
$result = ...

while ($row = $result-fetchRow())
{
  $recipients-addTo($row['address'], $row['name']);
}
@$result-free();

try
{
  $swift = new Swift(new Swift_Connection_SMTP('localhost'),
'your_domain');

  set_time_limit(0);
  $swift-log-enable();

  /* 100 mails per batch with a 60 second pause between batches
   */
  $swift-attachPlugin(new Swift_Plugin_AntiFlood(100, 60),
'anti-flood');

  flush();

  $message = new Swift_Message('your subject');

  $message-setCharset('utf-8');
  $message-setReplyTo(...);
  $message-setReturnPath(...);
  $message-headers-set('Errors-To', ...);

  $message-attach(new Swift_Message_Part($plain_content));
  $message-attach(new Swift_Message_Part($html_content,
'text/html'));

  $num_sent = $swift-batchSend($message, $recipients, new
Swift_Address(..., '...'));

  $swift-disconnect();

...


This is a rough example taken from my own script. This would need to be
modified if you're not sending the same message to all recipients, of
course. It's not clear to me from your example.

b



Nice! I'll have to look into this library some time. How do you
control it to prevent sending the same message though?
  

I'm not sure about that. Mine is for a newsletter (not personalised) so
Swift just needs a list of addresses. I can't remember if AntiFlood can be
used for many unique mails. I suspect that it doesn't.



I can't imagine
this is called from a web page, because I'm guessing it would take a
few minutes to finish.
  

cron. But, if you wanted it fired from an admin page (I can't imagine a good
situation where something like this would be public) you could add
ignore_user_abort() and then jazz up the page with some async JS.

If it's called from a cron job, don't you still


have to somehow flag the message as having been delivered so that the
next process doesn't come along and send the same thing all over
again?
  

Yeah, I just twigged to the fact that your messages are stored in the
database. This is some kind of messaging system? As opposed to a
newsletter-type thing, I mean. As above, I don't know that AntiFlood  is
what you want. That's meant for multiple-recipient mails.

There's also a Throttler plugin, though, again, it's meant for batches.

You might also look at the IMAP functions. Instead of storing the messages
in the DB, you can let the MTA deal with pushing them out and cut Swift (and
the cro0n job) out of the loop.

b




The case I have in mind is for a newsletter, and in this instance they
are all the same. In this instance, I am limited by the host to 2500
messages/hour. (Originally it was 900). At this point, 2500 is more
than enough for this group, but I'd still like to play kindly with the
server resources since this is a shared host. I just set up a generic
message queue and dump mail there rather than sending it real time.
For now, all the messages in a given batch are the same, but I was
leaving flexibility in case I hit a situation where that changed.

It looks like this would work pretty well when I'm up for a rewrite. :)

I understand the general idea:

1. read message body from storage (file, database, etc.)
2. get result set containing addresses from database
3. build recipient list from result set
4. send everything to sendBatch()

It seems to me that step 5 still has to either (a) delete the message
body from storage or else (b) flag it as sent so that the next process
spawned via cron sees that there is no work to do and dies rather than
repeating the whole process again.

Andrew
  


Thanks to everybody.
I have figured this out. I am using swift but I am limiting the send by 
a 100 and setting the table with the date sent. Then the crontab runes 
again and grabs another 100 that have not had the date set. This 

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

2008-07-31 Thread Chris
brian wrote:
 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.
 
 There's no need to limit the DB query, nor to track what's been sent by
 updating the DB. Grab all of the addresses at once and let SwiftMailer
 deal with the throttling:

Of course there is.

1) What if the server gets rebooted in the middle of the send or the
database goes down in the middle? You send to everyone again.

2) What if the host has a limit on how long a script can run for? You
send to everyone again and again and again each time the script tries to
start.

3) What if safe-mode is enabled on the server? You have a limited send
time and possibly end up with the same outcome as #2.

4) What if you are sending to 100,000 email addresses? You run out of
memory because you have so much data loaded.

-- 
Postgresql  php tutorials
http://www.designmagick.com/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



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

2008-07-29 Thread Richard Kurth
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


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



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

2008-07-29 Thread Andrew Ballard
On Tue, Jul 29, 2008 at 4:52 PM, Richard Kurth
[EMAIL PROTECTED] 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


I've done something very similar. I have a delivery timestamp column
in the table that is initially NULL, and I set it to UTC_TIMESTAMP()
for each row as I send the message. My query looks like this then:

SELECT * FROM mail_queue WHERE delivery_timestamp IS NULL LIMIT 100.

Andrew

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



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

2008-07-29 Thread Richard Kurth

Andrew Ballard wrote:

On Tue, Jul 29, 2008 at 4:52 PM, Richard Kurth
[EMAIL PROTECTED] 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




I've done something very similar. I have a delivery timestamp column
in the table that is initially NULL, and I set it to UTC_TIMESTAMP()
for each row as I send the message. My query looks like this then:

SELECT * FROM mail_queue WHERE delivery_timestamp IS NULL LIMIT 100.

Andrew

  

Then what do you do to get the next 100 emails without sending the same ones

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



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

2008-07-29 Thread Micah Gersten
Here's a PEAR package that handles this:
http://pear.php.net/package/Mail_Queue/

Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com



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



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



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

2008-07-29 Thread Stephen

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





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



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

2008-07-29 Thread Chris

 I've done something very similar. I have a delivery timestamp column
 in the table that is initially NULL, and I set it to UTC_TIMESTAMP()
 for each row as I send the message. My query looks like this then:

 SELECT * FROM mail_queue WHERE delivery_timestamp IS NULL LIMIT 100.

 Andrew

   
 Then what do you do to get the next 100 emails without sending the same
 ones

Re-read the reply.

After each email is sent, the timestamp is set.

The original query looks for timestamp fields that are not set.

-- 
Postgresql  php tutorials
http://www.designmagick.com/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php