Forum Moderators: coopster
The form finds all the email addresses in a MYsql database
and sends it two anyone who has a two bedroom apartment.
Works find however because it takes a long time to process nearly 1 minute the browser times out and shows page not found.
I am using
Header("Location: $fredirect");
to redirect the user to a success page.
How can I solve this problem?
the header("location: $fredirect"); is at the bottom of the script as the last thing it reads should I move this to the top before the email command or will it cause a conflict where by the emails wont be sent because a new page was displayed
Any adice would be grateful
In situations like this I've always setup some type of daemon process. On the form you simply insert the request into the database is a "pending" table.
From there the easiest thing to do is setup a cron script to fire every 5 or 10 min to process "pending" requests. This way user doesn't have to wait and no browser timeouts. There is a slight lag in the emails going out but nobody will notice I'm sure.
daisho.
Every night might be too long in this case but you could decide on a logical interval maybe every hour.
You could also use the syslog but I doubt you would have access to that unless you run your own server. The thing is to make it fire and forget or the quickest return possible. All the other processing can be done afterward by your parser.
Sending emails would be best left to another script as daisho and jatar_k have suggested.
eg. your current script keeps adding records about whom to send email .. and another script runs every minute/hour/day from cron to see what jobs are there to do .. and does them.
Also if you want .. you can setup data tables in a way that avoids duplicate emails getting sent (because of refresh by users)...by using a unique key appropriately.
1 - make the script recursive, i.e. it sends one email, then calls the same script again, whic sends the second email, calls the script again, etc... until finished - when it redirects. this could be done with passing a counter
pseudocode: send email to address $a, redirect to $PHPSELF?emailid=$a++, unless $a=final email... in which case redirect to success
2 - edit your php.ini file to increase the execution time.
;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;
max_execution_time = 3000 ; Maximum execution time of each script, in seconds
3 - include a number of email addresses in the bcc section of the email and then let the smtp server split it up and send it out
However it would stop me writing adding personal information into the emails. For example
The script extracts the row of an owner and writes into the subject field their ref: number, url and so on.
But the bcc idea seems to be the solution I will have to make a universal email and make it send to all the owners with just the content of the form.
Thanks for the help and ideas.
tblLeads:
id ¦ name ¦ email ¦ notes ¦ etc ¦ etc
The table schema would be the same as all the fields that you are posting with the form. Basically save all the form fields to a row in the database.
Then every 10min or so in a cron do something like: