0

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.

Giacomo1968
  • 23,903
  • 10
  • 59
  • 92
S Vinesh
  • 521
  • 1
  • 4
  • 15

5 Answers5

4

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

Community
  • 1
  • 1
Albert James Teddy
  • 1,161
  • 1
  • 11
  • 21
4

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.

Dinesh Rawat
  • 1,107
  • 8
  • 22
2

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.

TribalChief
  • 787
  • 5
  • 11
2

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

Community
  • 1
  • 1
0

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

AkshayP
  • 2,053
  • 2
  • 15
  • 26