tags:

views:

602

answers:

5

I develop a webpage where I need to send an email to a particular user [depend upon the database value] at a particular time automatically [without human interference].

For example I have a file named Send_mail.php. It will execute once per day by script [Not by Cron or other way]. Please guide us in this.

Thanks in advance.

+1  A: 

You're saying 'not by cron'. Why? Cronjobs are meant exactly for such cases! There is no better way to do that!

tharkun
+1  A: 

Unusual to not use cron for this. If it is web accessible you could trigger a script to http://yourwebsite.com/Send%5Fmail.php. This could be from any computer using software that is scheduled to perform a GET request ever day, etc. This is a client based request.

Pasta
A: 

You can use cronjobs to do this.

If cronjob is not available, and if the site is visited at least once a day, then put the code in the page. Every time the page is loaded, it checks if it should send an email to the user, or if an email has already been sent this day.

Marius
Possible but not reliable. You would need to be sure that your page is visited frequently enough.
tharkun
No , the site won't visited once a day . it's depend upon the user .
Sakthivel
@Sakthivel: there you're wrong. it would be enough, if the site is visited by anyone. the rest would be done my your script.
tharkun
Ofcourse , But if no one can visit the webpage for a week means ?
Sakthivel
+2  A: 

The only option that I can think of that would not invoke cron in anyway would be to have a script that, once triggered, would continue to run indefinitely. I agree with everyone else so far, though, that this is not what php scripts are meant to do and it is what cron is explicitly meant to do.

But to do it with just php would be pretty tricky. You would have to start the script in such a way that it wasn't from a browser (as closing the browser would kill the script), and wasn't from the command line (unless the user that initiated the script was always logged in server-side).

Not to mention that running the script in that way would probably add up over time in terms of memory use and might slow any other activity down to a crawl, which isn't what you want, I imagine.

Basically, php scripts run when something tells it to. If you want it to run reliably on a per day basis, something has to open the script. If you have no assurance that someone will visit some page on the site at least once (so that you could use Marius' method), then you'd have to have some server-side program run it for you (which sound a lot like cron to me).

You could have cron open the script on a per day basis if your concern is not with cron but only with having the script do all the work.

Anthony
+1  A: 

If not by cron you need, at the very least, something else to put the wheels into spin once a day.

Something reliable, able to run unattended for days, months, years, until the heat death of the webserver.

You could write it. In php, no doubt. That would be a daemon process on your webserver.

It would have to be started at each startup. It should be checking when then last job ran and decide if it's time to run another one. Being daily, it could sleep() for hours (say 1 or 2) and check if sending time passed, then run its job and go back to sleep.

It cannot be run inside apache mod_php or through fastcgi. Those scripts die after a little time. Say 30 seconds as a rule of thumb.

It could be started via the webserver program, instead of at startup time, yes, through a system() call to a nohup on a php command line executable, something like:

<?php system('nohup php jobrunner.php'); ?>

jobrunner should be checking if other copies of itself are running already and in that case commit suicide. It should not be startable or restartable by a web user without entering a password.

And being developed anew from scratch it could have new bugs and security risks involved.

So, yes, it could be done.
And, no, I do not think it should be done, if not for a giant project needing very custom handling of daily jobs.

In general, cron is secure, fast, mantained, known to system administrators, well accepted, already running and available at cheap hosting companies that give no command line or system() access.

ZJR