fullyloaded has asked for the wisdom of the Perl Monks concerning the following question:

hi im currently working on a project, i basically have a mailinglist for my site and i want to be able to send out my emails to people that join my site, but im having some problems when it comes to sending the emails. in my script i added a delay between sending large amounts of emails, this is to lighten the load on the server, so the whole process of sending the emails may take a while (providing i have that many people sign up), the problem is that when i test my script and it is sending out emails and waiting, the broswer times out before the perl script is finished processing, the broswer times out after about 4 minutes. i know there is a way to prevent browser timeouts but i dont know how? thanks anyone :)

Replies are listed 'Best First'.
Re: how to prevent browser timeouts
by merlyn (Sage) on Feb 05, 2002 at 23:28 UTC
    Two pieces of advice:
    1. Don't use CGI for things that aren't for CGI responses. Sending batches of email is much more of a command-line thing than a CGI thing.
    2. Learn about fork. Many of my columns have examples.

    -- Randal L. Schwartz, Perl hacker

Re: how to prevent browser timeouts
by rjray (Chaplain) on Feb 06, 2002 at 01:51 UTC

    To elaborate on Randall's response, what you should consider doing is something along the lines of:

    1. Present the fill-in form.
    2. When processing the "submit" button, do any data-verification or sanity-checking that you feel is needed.
    3. Fork a child process to take care of the mailing itself. Whether you choose to do the forking in a true "clean" way is up to you, and not important here.
    4. The original process then displays a "Thank you" page, telling the user that their data is being processed and they'll receive mail at a future point. That part is now done. It will do nothing else until called again by another form-submit.
    5. The second process (the child) handles the mail task, whatever that entails.

    Relevant part here is that the CGI application should not be doing silly things like "sleep" when there is a use on the other end of the wire waiting.

    However, I must ask this: what point is there in waiting some period of time before sending the e-mail? All invocations of this script will be waiting as well. You will still have the problem of a flood of requests flooding the mail server, just with an n-second delay. If you are that concerned about about potential mail flooding, then you have a different sort of application to consider.

    --rjray

      ok thanks heaps guys, thanks rjray you cleared alot of stuff up that i wasnt sure about. your right about the waiting period, and my broswer was waiting minutes before the perl script was finished sending the emails (very inneffcient!) i am new to this mass mailing stuff and im learning as im going. someone else on another forum suggested a put a delay of a second between sending each email, it sounded like the right thing to do but it just doesnt work for what im doing (as im making the request from the browser). I did look at a number of professional programs like "subscribeme" that do mass mailings and none of them mentioned anything about forking a process, so i had no idea i would need to do this, it does make sense and it does solve my problem so thanks heaps you have really saved my butt! excellent learning excercise :)
Re: how to prevent browser timeouts
by hopes (Friar) on Feb 06, 2002 at 01:12 UTC
    Hi,
    Are you system administrator in the machine which have the script?
    Your question "is not perl related" I think. You can configure the script timeout in webserver's configuration file.
    As merlyn said, you are trying to do tasks that isn't supposed to do by a CGI.
    Use better a non CGI script to do this jobs.

    Hopes
    $_=$,=q,\,@4O,,s,^$,$\,,s,s,^,b9,s, $_^=q,$\^-]!,,print
Re: how to prevent browser timeouts
by dws (Chancellor) on Feb 06, 2002 at 01:21 UTC
    One simple technique for keeping the browser happy while a long-running CGI is cranking away is to unbuffer STDOUT, then periodically print   <!--ping--> It's a kludge, but it works.

Re: how to prevent browser timeouts
by Fastolfe (Vicar) on Feb 06, 2002 at 22:01 UTC
    I might suggest Mail::Bulkmail. You're probably better off queueing the request via CGI and performing the actual sending of mail in a separate job, either forked from your CGI or spawned independently (e.g. a cron job set by the CGI or checking for the presence of a flag set by your CGI).