batch emailing

stevet70

New Member
Messages
35
Reaction score
0
Points
0
here's the main chunk of code to show where I'm up to, using PHPMailer (v5) to allow sending out html emails to a database held mailing list

Code:
$result = @mysql_query($query);

while (1==1) {
  set_time_limit(30); // sets (or resets) maximum  execution time to 30 seconds)
  // .... put code to process in here

if (!$result) {
     exit(mysql_error());
}

while ($row = mysql_fetch_array ($result))
{

	$mail->Subject  = "$subject";
	$mail->MsgHTML($Header.$Body.$FooterStyle.$Footer.$Close);
    $mail->AltBody = $text_body;
    $mail->AddAddress($row['email'], $row['first'] . " " . $row['last']);

    if(!$mail->Send())
        echo "There has been a mail error sending to " . $row['email'] . "<br>";

  usleep(1000000); // sleep for 1 million micro seconds - will not work with Windows servers / PHP4
  // sleep(1); // sleep for 1 seconds (use with Windows servers / PHP4
  if (1!=1) {
    break;
  }

    // Clear all addresses and attachments for next loop
    $mail->ClearAddresses();
    $mail->ClearAttachments();
  }
}

whilst it does actually work and I've experimented with the 'unsleep' element to adjust how quickly emails are sent out one after another I hit a not so slight snag with my hosting company temporarily suspending the account due to me overloading their server!

I'm guessing the set_time_limit(30) could be doing bad things, more so than the unsleep(1000000) but I'm not sure of the best way to send out what could be a few hundred emails in a safe and sensible manor - avoiding being labelled spam and / or annoying the hosting company.

Any suggestions?
 

garrettroyce

Community Support
Community Support
Messages
5,611
Reaction score
249
Points
63
What Simplemachines Forum (and many others, I assume) does is to send out a few e-mails (I think mine is set to 1) every time a user loads any page of the website. This distributes the load more evenly across many members instead of one big burst on one member. You just create a mail queue using a database.
 
Top