#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    367
    Rep Power
    46

    Sending groups of emails


    What's the best way to send a batch of e-mails from a php script without overloading the server or exceeding a hosting company's volume limit?

    I have a script that will generate an email for about 500 addresses. Is there a good method for batch sending over an extender period of time?

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,868
    Rep Power
    368
    You could sleep for x amount of seconds (where x can vary using rand() between 1-20sec) every 1/5/10/100 sends..

    You could also use an external company, phpmaillist or something like that which a prev company I worked for used to send emails.
  4. #3
  5. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    400
    Rep Power
    311
    in a nutshell: use swiftmailer or phpmailer or something like that to do the sending, queue up the mail in your database, and send them in batches via cron. If you're likely to be sending massive amounts of email in the future then this wont scale well, so i'd look more into message queuing etc.
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    367
    Rep Power
    46
    Originally Posted by paulh1983
    You could sleep for x amount of seconds (where x can vary using rand() between 1-20sec) every 1/5/10/100 sends..

    You could also use an external company, phpmaillist or something like that which a prev company I worked for used to send emails.
    OK, Thanks.

    I had considered this but the only problem I can see is that it would cause the script to run a long time. This is not a problem for me but would it cause the server to think that the script had stalled or that it is consuming too much bandwidth?

    Thanks again
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    367
    Rep Power
    46
    Originally Posted by sir_drinxalot
    in a nutshell: use swiftmailer or phpmailer or something like that to do the sending, queue up the mail in your database, and send them in batches via cron. If you're likely to be sending massive amounts of email in the future then this wont scale well, so i'd look more into message queuing etc.
    Thanks.

    I think I should be considering the scaling aspect (always optimistic). Can you point me to e reference for message queuing?

    Thanks again.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,232
    Rep Power
    593
    A sleeping script will not cause the server to take any detremental actions against that process. The only concern might be the launch of a second script before the first has completed, if that is a possibility. You would need to take that into account when writting the script by some kind of locking mechanism.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    367
    Rep Power
    46
    Originally Posted by gw1500se
    A sleeping script will not cause the server to take any detremental actions against that process. The only concern might be the launch of a second script before the first has completed, if that is a possibility. You would need to take that into account when writting the script by some kind of locking mechanism.
    Thanks
    I assume you mean spawning a second script from the same browser window/tab. If the email script opens in a new tab but then I move to a different script on the first tab, that shouldn't present a problem. Correct?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,232
    Rep Power
    593
    From a system perspective, no. However, long running scripts launched from a browser are a different story and can be problematic. I would suggest you launch the script into background and just report a successful start to the browser. You don't want the user to get impatient and resubmit the form that kicks off the script with the same parameters a 2nd time. Another issue is if a 2nd script is launched with different parameters, would it result in both scripts working on the same emails? That is the question you need to keep in mind when you program it.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    367
    Rep Power
    46
    Originally Posted by gw1500se
    From a system perspective, no. However, long running scripts launched from a browser are a different story and can be problematic. I would suggest you launch the script into background and just report a successful start to the browser. You don't want the user to get impatient and resubmit the form that kicks off the script with the same parameters a 2nd time. Another issue is if a 2nd script is launched with different parameters, would it result in both scripts working on the same emails? That is the question you need to keep in mind when you program it.
    Sounds like running the script in the background is a good solution.

    Not having done that, I searched the web for a sample and found the following:
    PHP Code:
    echo "parent started...\n";
     
    exec(scriptname.php " > /dev/null &"); 
    echo 
    "parent finished.\n"
    Is that the correct syntax?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,868
    Rep Power
    368
    you could run a cron job which runs every two hours or something.. and there might be a way to try to get a lock on this cron job so if it is running dont run it again. (My host does this automatically for me so when i set up a cron job so no job runs twice in parallel)
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    367
    Rep Power
    46
    Originally Posted by SeanF
    Sounds like running the script in the background is a good solution.

    Not having done that, I searched the web for a sample and found the following:
    PHP Code:
    echo "parent started...\n";
     
    exec(scriptname.php " > /dev/null &"); 
    echo 
    "parent finished.\n"
    Is that the correct syntax?
    Thanks, everyone for your replies.

    Could someone let me know if the "exec" syntax I've outlined above will work? Or am I missing something?

    Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo