+ Reply to Thread
Results 1 to 2 of 2

Thread: batch emailing

  1. #1
    stevet70 is offline x10Hosting Member stevet70 is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    35

    batch emailing

    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?

  2. #2
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: batch emailing

    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.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

+ Reply to Thread

Similar Threads

  1. Contact.php Email Form Not Emailing
    By bigjohnsta in forum Free Hosting
    Replies: 5
    Last Post: 05-12-2009, 06:14 AM
  2. Simple Batch Question...
    By Twinkie in forum Programming Help
    Replies: 7
    Last Post: 03-22-2009, 04:43 PM
  3. Batch File
    By varunrai in forum Programming Help
    Replies: 1
    Last Post: 01-08-2009, 05:08 AM
  4. Trouble with webpage forums and emailing.
    By abarry in forum Programming Help
    Replies: 4
    Last Post: 09-20-2008, 09:26 PM
  5. Batch on a Mac
    By MarioMaster in forum Computers & Technology
    Replies: 1
    Last Post: 04-04-2008, 03:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers