在PHP中的特定日期自动发送邮件

时间:2014-06-20 04:02:25

标签: php email

我的数据库中有一些邮件ID。我必须在我的数据库中提到的某些特定日期自动发送邮件给那些邮件ID。如何在php中执行此操作。

5 个答案:

答案 0 :(得分:4)

这取决于您的服务器类型。如果你使用linux,你可以使用cronjobs在特定时间编程特定php文件的执行。 如果您是托管的,您的主机可能会在其cpanel中提供cronjob菜单,否则您必须获得更好的托管计划。或至少访问crontab文件,您可以在其中编写不同的Cronjobs。

Executing a PHP script with a CRON Job

答案 1 :(得分:4)

您需要编写cron作业,以便在特定日期发送自动电子邮件。如果没有这个,就不可能。

<强>语法 这是一个简单的cron工作:

10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1

主要有两个部分:

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.

计时语法

这是cron作业字符串的第一部分,如上所述。它决定了cron作业的运行频率和时间。

它由五部分组成:

minute
hour
day of month
month
day of week

您可以在cpanel中设置cron。

答案 2 :(得分:2)

以下是您可以遵循的逻辑的一些常规步骤:

PHP脚本应该:

  • 从数据库中提取电子邮件地址和特定日期 进入阵列。
  • check是否 今天的date是 在包含日期的数组中。请务必妥善设置日期格式!
  • 遍历电子邮件地址(最好是foreach()循环), 如果上述检查返回&#39; true&#39;。
  • 在此循环中,使用PHP的mail()函数发送您的电子邮件。

您的下一步是设置一个每天运行此脚本的计划任务,通常是通过cron作业。此步骤取决于您获得的服务器。

注意:这些步骤说明了执行所需操作的最基本方法。可能还有其他更复杂的方法可以更快,更清洁,更少密集在您的服务器上。

答案 3 :(得分:2)

正如Albert James所说,它取决于运行应用程序的系统类型。如果它是Linux,那么你可以获得发送由cron作业执行的邮件的php脚本,如果你使用的是Windows机器,那么你需要执行带有日程安排任务的php脚本:How to run a PHP file in a scheduled task (Windows Task Scheduler)

如果您不想使用计划任务\ cron作业(虽然我还没有使用过),也可以选择以下选项:How to send schedule emails using php without cron job

答案 4 :(得分:0)

软件实用程序Cron是类Unix计算机操作系统中基于时间的作业调度程序。设置和维护软件环境的人使用cron来调度作业(命令或shell脚本),以便在固定的时间,日期或间隔周期性地运行。

<强> example