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.
-
Take a look at `crontab` – Pierrickouw Jun 20 '14 at 04:06
-
Can you please mention the exact link – S Vinesh Jun 20 '14 at 04:09
-
So what have you done so far to solve this issue? – Giacomo1968 Jun 20 '14 at 04:13
5 Answers
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.

- 1
- 1

- 1,336
- 1
- 13
- 24
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.

- 970
- 10
- 32
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.

- 797
- 5
- 11
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

- 1
- 1

- 21
- 2