SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Jul 2006
    Location
    jhb, south africa
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Mass mailing with PHP

    I would like to know if anyone has had any experience with using PHP to send mass emails (+- 4000 emails). I'm trying to find a method to avoid server load, and to professionally and efficiently handle the mailing.

    My first method was to send a batch of emails at a time. So it would loop through 30 emails and send the mail off using mail() function, and update the 'sent' field for the email. Once the batch of 30 emails were sent, I would output some javascript to reload the page after 5 seconds.
    The script would then load the next 30 unset emails and the process is repeated until all emails have been sent.
    This method is obviously not the best as you need your browser open to continuely load the sendmail script, and the entire process may take up to 2 hours.
    With using this method, the execution time to send 30 emails was anywhere from 2 seconds to 64 seconds..

    So to avoid execution time exceptions, and to make the process run by the server and not the client, I thought of using cron jobs, although i've never dabbled with cron before.

    I was wondering if anyone had any suggestions, or if you could point me the right direction it would be greatly appreciated.
    Many thanks, Rich

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, you should use a cron job. Just use the same script, only with no output and set it up in cpanel for example.
    Saul

  3. #3
    SitePoint Member
    Join Date
    Jul 2006
    Location
    jhb, south africa
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thankyou for your reply. With using a cron job, does execution time come into play?
    As mentioned in my previous post, execution times vary for the same amount of mails sent, perhaps mail server gets overloaded..
    I would also like to sleep() the script between sending emails, to avoid any sort of server overload, but then it would take the script up to 2min to finish executing, would php ini max_execution_time affect this?

    I was also wondering, does anyone know of any articles that explain how to log returned reciept mails?

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes.
    you could
    set_time_limit(0);
    ignore_user_abort(true);

    if running via cron, you usually use the php CLI, which has no time limit (by default, your host could set one). if you trigger your script via wget, then youre not using php's CLI version.


    the mail() function isnt suited well to bulk mail. use a mailing library like swiftmailer, or pear MAIL/MAIL_QUEUE will improve performace because you do not need to keep opening and closing an smtp connection for each email.

  5. #5
    SitePoint Member
    Join Date
    Jul 2006
    Location
    jhb, south africa
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thankyou very much for swiftmailer recommendation, I have had a quick look and it seems like a solution to my problem..
    Previously I was using the HtmlMimeEmail class from phpguru's.. but this package looks a whole lot more efficient..
    Thanks once again
    whatever the mind
    can conceive and believe
    it can achieve

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to add to what clamcrusher has already said it is best not to change the php.ini settings for your whole site but use set_time_limit instead.

    I've written a tutorial on using cron with php. The approach I use is to open up a small web browser and call the script just as you would any other page.
    Web Development Blog - Useful bits & pieces

  7. #7
    SitePoint Member
    Join Date
    Jul 2006
    Location
    jhb, south africa
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I might be wrong here, but i'm sure i remember playing around with large file uploads and found that set_time_limit(0) for example would not actually work, and this would have to be changed in the php.ini settings... Is this different for scripts executed by cron schedule?

    Thanx for the tutorial streety, seems to be a simple process to setup a schedule.. will probably do this through cpanel as php_daemon mentioned.
    whatever the mind
    can conceive and believe
    it can achieve

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for file uploads theres other settings usually at hand.

    max_input_time
    upload_max_size
    post_max_size

    all of which must be set in php.ini, or in httpd.conf/.htaccess
    reason being it wouldnt make sense to set them after the script started being parsed/executed, and any settings made inline in your script arent read until well into the scripts execution phase.

  9. #9
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also the file upload happens before your script gets a chance to call set_time_limit(), so it's effectively useless in that situation.

    When dealing with mail via cron you can call that function before the sending starts.

  10. #10
    SitePoint Member
    Join Date
    Jul 2006
    Location
    jhb, south africa
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ^^ thanx for clearing that up
    whatever the mind
    can conceive and believe
    it can achieve


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •