Is it possible to split an e-mail addressed array into batches ?

Hi all,

I got an application which sends e-mail to an array of users --> $email_to = {email_to} .
{email_to} can have also more that 2000 addressee
I would like to insert a sleep(1800) within the routing of 100 email to avoid blocking in e-mail sending but I need to split the array of {email_to} into batches of 100 email at time.
I don’t know how achieve it…

More or less I have this parameters to use.

$email_to = {email_to}; // To email array
$nr_email_to = $count = count($email_to); //Total Nr of email to send
$nr_mail_batch = ceil($nr_email_to/100); // Nr of batches to send

sc_mail_send([glo_mail_smtp_server], [glo_mail_smtp_user], [glo_mail_smtp_pass], $mail_from , $mail_from , {subject}, $message, “H”, $email_to , $type_copy , [glo_mail_smtp_port],[glo_mail_smtp_ssl], $email_attach);

Probably it’s completly wrong… as approch becouse I’m not a programmer
Here , I suppose, I need to have another internal loop or similar that send 100 email at time splitting the ‘$email_to’ array correctly otherwise I send all mails everytime … ;-((

$x=1;
while($x<=$nr_mail_batch)
{
???
sc_mail_send([glo_mail_smtp_server], [glo_mail_smtp_user], [glo_mail_smtp_pass], $mail_from , $mail_from , {subject}, $message, “H”, $email_to , $type_copy , [glo_mail_smtp_port],[glo_mail_smtp_ssl], $email_attach);
???
sleep(1800)
$x++;
}

if you have a better solution it will be very appreciated
Googling I saw also array_chunk($array, 100); but I don’t know if it could be helpful or not.

Well you could actually do that. Some smtp servers do not allow so any (100) to addresses at once. So you may have to split it in less.
I personally would do that via phpmailer and have a simple php backgound job running that checks for records in a table. Doing a sleep in php would not be my favorite choice in a web application since it sets a process temporarily on halt (might cause web page timeouts and so on).
In fact we send bulk mail via phpmailer via a scheduled job. See http://sourceforge.net/projects/phpmailer/
Then just have a php program that basically works like this (in pseudo code):

check MailScheduler table for records not having an IsSent set (perform the select * from MailScheduler where IsSent!=‘Y’)
counter=0;
for all records do{
if (counter==100){
sendmail to userlist;
if (mail is sent ok){
update MailScheduler set IsSent=‘Y’ where mailid=userlist
userlist:=empty;
}
sleep 600; //that is 600 seconds
}
add user from sqlquery to userlist userlist;
counter++;
}
if (userlist!=empty) {send mail to all userlist);

Because it is a scheduler job you wont have interference in your scriptcase website. And you can even run it in low priority.

Hi,
array_chunk() should do the trick as well, but rr is right, you might run into trouble with the timing.

$chunk_size = 100;
$i = 0;
$email_to = array_chunk($email_all,$chunk_size);
while(isset($email_to[$i]))
{
sc_mail_send(…, $email_to[$i], …);
sleep (600);
$i ++;
}

jsb

Thanks rr and jsb !!
Unfortunately to implement a library it’s not a piece of cake for me… ;-((
When you say ’ it sets a process temporarily on halt ’ means that the user that has run the bulk mailing has to wait until all the mails will be sended or also other user are blocked in their activities ?
If they send for istance 2000 mails with only text and no attachment , how much could be , more or less, the time the user has to wait ?

Hi,

I implemented the code but I got an error on chunk (string-array) so I added explode

$email_to_all = {email_to}; {email_to} is a double select field and it’s the list of ‘all’ the to email

I changed the line to $email_to = array_chunk(explode(’;’,$email_to_all),$chunk_size); (I hope it’s correct…)

I did a test (only 3 email addresses just to see if there are errors) but when I click on OK sending button the application goes in connecting an stay there … forever.
The only way to exit is to close the browser.

//Code used

$chunk_size = 100;
$i = 0;
$email_to = array_chunk(explode(’;’,$email_to_all),$chunk_size);
while(isset($email_to[$i]))
{
sc_mail_send([glo_mail_smtp_server], [glo_mail_smtp_user], [glo_mail_smtp_pass], $mail_from , $mail_from , {subject}, $message, “H”, $email_to[$i] , $type_copy , [glo_mail_smtp_port],[glo_mail_smtp_ssl], $email_attach);
sleep (20);
$i ++;
}

if the php goes to sleep then that part doesnt run further for so many seconds. Meaning the user doesnt get feedback from parts of the page. I would avoid that at all costs therefor I opt for phpmailer. Believe me phpmailer is easy enough to implement. Do a simple search on how to use phpmailer and you will find: http://stackoverflow.com/questions/1318861/how-do-i-use-phpmailer-cant-find-a-simple-decent-tutorial-online

The user would likely wait for a reply from the server. Yet the reply doesnt come in time since some brwosers expect a reply within x seconds (definitely less then 60) and will thus timeout. THat is not what you would want. In fact the phpmailer option is therefore better.

Also see: http://phpmailer.worxware.com/?pg=tutorial

Just to understand…

Do I need to add the class under C:\Program Files (x86)\Scriptcase\v71\wwwroot\scriptcase\app\my_application\php_mailer ?

I used include in past but never require : do I use require in the same way of include ----> require("…/php_mailer /class.phpmailer.php");

that depends where you want to run it. In a seperate job you can simply put the classes in your doc_root dir. Basically for running it from scriptcase you can do the same. The easy way is to put the classes in your php dir…

Hi rr,
I did some try and the best that I’ve reached so far is this error…

“Invalid address: ‘giovanni.aaaaaa@libero.it;paolo.abcdef@gmail.it;carlo.jdjdjdj@yahoo.com’ You must provide at least one recipient email address. Message Not Sent
Mailer Error: You must provide at least one recipient email address.”

I take the addresses from double select field that output the list in this way: email@xxx.com ;email@yyy.com;… and so on .
‘giovanni.aaaaaa@libero.it;paolo.abcdef@gmail.it;carlo.jdjdjdj@yahoo.com’
I really don’t know how fix the error

Hi,
there’s a blank in the email address.

c arlo.jdjdjdj@yahoo.com

It 's an editor problem only

Unfortunately is a problem of editor here that make strange format when you copy e-mail addresses
In realty there are no blank

I think that phpmailer wants another kind of string structure itself

[QUOTE=jsbinca;21729]Hi,
there’s a blank in the email address.

c arlo.jdjdjdj@yahoo.com[/QUOTE]

Sorry rr but it’s to complex to understand to me… ;-((
How phpmailer expect to be the to addresses ?

Hi Jsb,

I tested the code and now it work . I know it’s not the best of solutions and has risk. Phpmailer could be better but I’m stucked on the error I posted
About the sleep(??) . To balance it for 100 mails how much have I to use (more or less…).
If I send less than 100 mails the sleep has no effect or I’m wrong… ??
Thanks

Hi,
you have the sleep at least one time no matter how many emails you have. To skip it on less then 100 you have to count your emails/chunks.

$c_count = count($email_to); // number of chuncks

In the loop:

if($c_count > 1 && $i < $c_count) //skip the sleep if only one batch or the last one
{
sleep();
}

jsb

This is last version:

Is it correct as your suggestion ?

$chunk_size = 100;
$i = 0;
$email_to = array_chunk(explode(’;’,$email_to_all),$chunk_size);
while(isset($email_to[$i]))
{
if(empty({email_attachment})){
sc_mail_send([glo_mail_smtp_server], [glo_mail_smtp_user], [glo_mail_smtp_pass], $mail_from , $mail_from , {subject}, $message, “H”, $email_to[$i] , $type_copy , [glo_mail_smtp_port],[glo_mail_smtp_ssl]);
}
else{
sc_mail_send([glo_mail_smtp_server], [glo_mail_smtp_user], [glo_mail_smtp_pass], $mail_from , $mail_from , {subject}, $message, “H”, $email_to[$i] , $type_copy , [glo_mail_smtp_port],[glo_mail_smtp_ssl], $email_attach);
}
if($c_count > 1 && $i < $c_count) //skip the sleep if only one batch or the last one
{
sleep();
}
else {
sleep (600);
{
$i ++;
}

Almost.

if($c_count > 1 && $i < $c_count) //skip the sleep if only one batch or the last one
{
sleep(600);
}

No else. Sorry my bad.

jsb

Hi Jsb,

I did the modification but now it says… variable $c_count not defined.

==========
$chunk_size = 100;
$i = 0;
$email_to = array_chunk(explode(’;’,$email_to_all),$chunk_size );
while(isset($email_to[$i]))
{
if(empty({email_attachment})){
sc_mail_send([glo_mail_smtp_server], [glo_mail_smtp_user], [glo_mail_smtp_pass], $mail_from , $mail_from , {subject}, $message, “H”, $email_to[$i] , $type_copy , [glo_mail_smtp_port],[glo_mail_smtp_ssl]);
}
else{
sc_mail_send([glo_mail_smtp_server], [glo_mail_smtp_user], [glo_mail_smtp_pass], $mail_from , $mail_from , {subject}, $message, “H”, $email_to[$i] , $type_copy , [glo_mail_smtp_port],[glo_mail_smtp_ssl], $email_attach);
}
if($c_count > 1 && $i < $c_count) //skip the sleep if only one batch or the last one
{
sleep(600);
}
$i ++;
}

Hi,
you need to set the variable.

$chunk_size = 100;
$i = 0;
$email_to = array_chunk(explode(’;’,$email_to_all),$chunk_size );

$c_count = count($email_to); // number of chuncks

while(isset($email_to[$i]))
{

jsb

OK , many thanks.
Now I’ll wait that a massive test woud be done !!
Bye