I am having some mail ids in my database. I have to send mail to those mail ids automatically on some specific dates that i have mentioned in my database.How to do that in php.
It depends on your server type. If you use linux you can use cronjobs to program the execution of a specific php file at a certain time.
If you are hosted, your host may offer a cronjob menu in their cpanel, else you would have to get a better hosting plan that offer that. Or at least access to crontab file where you program the different Cronjobs.
Executing a PHP script with a CRON Job
You need to write cron jobs for sending the automatic emails on a particular date. Without this it is not possible.
Syntax
Here is a simple cron job:
10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1
There are two main parts:
The first part is "10 * * * *". This is where we schedule the timer.
The rest of the line is the command as it would run from the command line.
The command itself in this example has three parts:
"/usr/bin/php". PHP scripts usually are not executable by themselves. Therefore we need to run it through the PHP parser.
"/www/virtual/username/cron.php". This is just the path to the script.
"> /dev/null 2>&1". This part is handling the output of the script.
Timing Syntax
This is the first part of the cron job string, as mentioned above. It determines how often and when the cron job is going to run.
It consists of five parts:
minute
hour
day of month
month
day of week
Or
You can set the cron in you cpanel.
Here are a few general steps of the logic that you can follow:
The PHP script should:
extract the email addresses and the specific dates from your database
into arrays.
check whether
today's date is
in the array containing the dates. Be sure to format the date appropriately!
loop through the email addresses (preferably in a foreach() loop),
if the above check returns 'true'.
within this loop, use PHP's mail() function to send your email.
Your next step would be to set up a scheduled task that runs this script daily, usually by means of a cron job. This step depends on the server you've got.
NOTE: These steps illustrate the most basic way to do what you require. There may be other, more complex ways that would be faster, cleaner and less intensive on your server.
As answered by Albert James it depends on the system type on which your application is running. If it is Linux then you can get the php script which send mail executed by cron jobs and if you are using windows machine then you need to execute that php script with schedule task : How to run a PHP file in a scheduled task (Windows Task Scheduler)
Also here is the option if you don't want to use schedule task\cron jobs (Though I haven't used that): How to send schedule emails using php without cron job
The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.
example
Related
My system is for reservations, and I want to send a reminder email 15 minutes before without using cron on Linux/Unix/BSD boxen nor Scheduled Tasks on Windows because I'm using mac and the users' PCs will be windows.
So, what should I do?
Initially, I compared all date and time with the ones in the data base but still nothing arrived.
Thanks in advance.
I tried if the date&time now = the date&time in the DB send a conformation email.
and about the corn I have 2 things:
1- I'm using mac -as a programmer- and if I want to implement this function I'll have to use Scheduled Tasks on Windows which - as I think- will be need working on a windows environment.
and the system will be installed on a Microsoft PC.
2- I have not dealt with corns before, and I'm afraid it'll take time that I don't have.
You have to use some kind of service or job to trigger the email to be send. Because php is self needs to know when to exec the code ("script").
Use CRON or Task Scheduler or build your own job / Service
For Scheduling jobs on linux you can you at command. Here is example
at 12:32 -m -f /usr/local/bin/backup-script
Here is good manual for at linux.
http://www.brunolinux.com/02-The_Terminal/The_at_Command.html
If you want to avoid cron job, then you have to depend on some hacky solution to trigger/call your "send email" code
like,
depend on site users. If they are coming on regular intervals every minute, check on each reservation the upcoming reservation time and email to them.
run a daemon process OR a function that keeps running for a specific period of time, say 24 hours, and check for upcoming reservations every minute or xyz time. You can put this process on sleep for a minute (or more) to check reservations on regular intervals.
use 3rd party cronjob scripts or hosted cronjobs. like https://www.setcronjob.com/ (needs $ for this solution) or onlinecrontab.com
I have three mysql tables; Users, Tasks and Task_Users, where each user can create their tasks through a web application I'm developing in PHP.
The thing is, I want my application to send an email to the users one day before the task occurs. The first thing that came to my mind was to do a MySQL job or trigger to send the email but I dont have an idea of how to do id,
What would be the best way make this happen or how could I call a job through PHP and execute it every day and verify that there is just one day left before the taks ocurrs and then if that is true send the email?
Here's my table definition. Thanks in advance.
Users
----------
UserId,Names,email,pass
Tasks
------------
TaskId,TaskName,Descripcion,TaskDateTime
Tasks_Users
------------
TaskId,UserID
You can write a CRON Job for this.
What's CRON Job ?
Cron is the name of program that enables unix users to execute commands or
scripts (groups of commands) automatically at a specified time/date. It is
normally used for sys admin commands, like makewhatis, which builds a
search database for the man -k command, or for running a backup script,
but can be used for anything. A common use for it today is connecting to
the internet and downloading your email.
What do you have to do ?
Just write a normal PHP script. make one that will work if it's launched directly from the browser. Then schedule that very same PHP file to run in cron, using this as a guide:
How to run a CRON Job after it's written ?
http://www.inmotionhosting.com/support/edu/cpanel/301-run-cron-job
If you have any issues let me know.
I'd make a cron job that regularly runs a script (say every 5min?) which checks for tasks that need emails sent. Then the script script sends the necessary emails.
*/5 * * * * php /path/to/script.php
Would call script.php, which checks and sends emails, every 5minutes.
Some info on cron is here:
http://www.pantz.org/software/cron/croninfo.html
I want to build an application that enables users to schedule emails to send any time. simply, write email message and schedule it so that the server sends it at time specified. I am using zend framework. How to do it in php? Can it be done with cron jobs? If yes, then what are the disadvantages of using cron?
can it be done with cron jobs?
Cron is a time-based job scheduler in Unix-like computer operating systems. The name cron comes from the word "chronos", Greek for "time".1 Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes, such as connecting to the Internet and downloading email.
http://ubuntuforums.org/showthread.php?t=586478
I would run a cron job every minute and check if there are any mails ready to be scheduled. The quote from the forum topic below instructs how to run cron every minute.
crontab -e
then set a tab like
* * * * * /command
The first star is the minute section,
so having a star there will execute
every minute
In case it makes it more clear if you
wanted every 5 mins then it would be
*/5 * * * * /command/to/execute
And the other stars are from left to
right
minute hour dayofmonth month
dayofweek*
*0=sunday
Disadvantages of cron?
if yes, then what are the
disadvantages of using cron
When doing a lot of cronjobs you will have to spawn a lot of processes(pay cost of spawning process which is expensive). In that case it would be better to have background process(es) running continually and fetch messages from message queue. But when you want to run a cronjob only ever minute than I assume this will not be a big case.
I would tackle this using a cron job.
Simply create a script that checks for messages to send at a certain time. The user schedules for say 1PM (using a database of course), the script runs every 5 min, or so, and it checks (the db), are there any messages to go out for the current time? If so, it sends out the emails, else it sleeps.
Clean and simple way of handling it.
Disadvantages?
I can't see any, this is what a cron is made for, running tasks at specific times.
i managed to send multiple emails (check here).i am stuck with sending automated emails via cron.
This is what i need - while the admin send emails, i store the message, emails, event date in the database. now i am trying to set a cron job to send emails to all these ids from the table with the message i have as a reminder. i am not familiar with cron job scripting, can anyone help in guiding me the right way to write script that i can place in cron tab. I am planning to send two mails - one day exactly before the event and on the day of event.thanks
Just write a normal PHP script -- make one that will work if it's launched directly from the browser. Then schedule that very same PHP file to run in cron, using this as a guide:
http://www.unixgeeks.org/security/newbie/unix/cron-1.html
Basically, using the values at the beginning, specify the schedule (minute, hour, day of week, day of month, etc.). Then set the user it runs as, which is likely to be "apache" or whatever your web server daemon is running under. Then set the "command" that cron is running to be php php_email_script.php (where "php_email_script.php" is the name of your PHP file.
30 minutes and still no answer, here's a few open doors:
cron reads it's rules from system-wide /etc/crontab, or from you personal crontab which you edit with crontab -e
cron takes a format where you say on which minute / hour / day / month things should happen, use google or man crontab for the format
cron has the amazing side effect of mailing the output of the command to the user owning the crontab
Now you are stating that you're using php. The easiest way to get some php running from cron, is to issue a wget -O - -q http://yoursite.com/yourprocessingscript.php?verysecret=123123 and have an appropriate processing script on yoursite.com. (you may want to let that script check $_SERVER['REMOTE_ADDR'])
So in short, if you just put the right magic in /etc/crontab, like
0 0 * * * jay wget -q -O - "http://yoursite.com/processmidnight.php?secret=yes_very"
and have your script produce some sensible output, you will get a mail delivered to local user jay, which you may want to forward.
I have some files on my server, how to open them programatically once a day?
Let them be
http://site.com/scripts/video.php
http://site.com/scripts/music.php
Without my hands, just like sheduling (automatically).
Even if I sleep and server is working, they should open on given time.
And additionally, how to open them once a 10 seconds (for tests)?
Thanks.
The Solution is very clear when you are using a Linux server;CRON JOBS.
One can easily run a cron job by configuring it through the terminal.I saw everyone has provided the Solution,but my answer will be for the people who are novice to Linux servers and don't know much about Cron Jobs.Go to Terminal and type the below commands..
root>which php
The above line will give you the path to where PHP is in your linux systems
Now,
root>crontab e
The above line will open the Cron file in edit mode.
Enter the number of times you want to run a particular php file and what time of the day,month,week,etc.
I am providing the syntex for running a particular file every 15 mins.
So here you go,
(write this in the cron file in edit mode)
*/15 * * * * path/to/your/php path/to/the/file/you/want/to/run
Now,path/to/your/php has to be replaced by the path what you got when you typed
root>which php
And you are done just save the file and close it.You will see a messege on you terminal that a new CronJob is installed.
That's it.
If you're on a Linux/Unix host using a cron job is generally the best approach, as you can simply call the command line version of PHP as a part of the cron job. (You may need to tweak your script if it relies on $_SERVER variables, that said.)
Administration middleware (such as Plesk) often offer the ability to add cron tasks as well, although you many need to check the user/group rights that such tasks are executed with.
Finally, if you use a cron task you can simply enter the required command via the command line during the testing phase. (i.e.: Rather than force a 10 second update (which would be tricky unless you had cron execute a shell script) you could execute the script as required.)
It's not possible with pure PHP. You'll need a cron job for this - ask your provider or administrator whether they are available.
Cron has a resolution of 1 minute, though: Calling a script once every 10 seconds would have to be done e.g. using a PHP script that gets called every minute, and makes six requests every ten seconds.
Running them once a day requires a seperate program running them.
For linux servers the usual choice is a Cron Job, for Windows the Task Sheduler works fine, too.