【问题标题】:send email automatically before the expire date在过期日期之前自动发送电子邮件
【发布时间】:2014-05-12 17:03:26
【问题描述】:

在我的应用程序中,由于会员资格即将到期,我需要将电子邮件自动发送给注册客户,cron 作业是自动发送邮件的唯一解决方案吗,我尝试了 cron 作业,我可以在其中发送邮件对于预定的时间,但我将如何获取客户的相应到期日期并触发邮件

    <?php

// Set this to your timezone
date_default_timezone_set('asia/kolkata');

// Start at 8:00 AM (24-hour time)
$startTime = mktime(8, 0, 0);

// End at 5:00 PM (24-hour time)
$endTime = mktime(17, 0, 0);


$currentTime = time();

// Do not send the email if it is outside of the allowed hours
if($currentTime < $startTime || $currentTime > $endTime)
{
    print('Not sending an email after hours.');
    die();
}

// Get the current day of the week as an index (0=Sunday, 6=Saturday)
$dayOfWeek = date('w');

// Do not send the email on weekends
if($dayOfWeek == 0 || $dayOfWeek == 6)
{
    print('Not sending an email on the weekends.');
    die();
}

// Info of person to receive the tests
define('TO_EMAIL',      'ramya.krish7@gmail.com');
define('TO_NAME',       'ramya');

// Info of person sending the tests
define('FROM_EMAIL',    'webmaster@serversendingtests.com');
define('FROM_NAME', 'Email Tester');

// Example: 8:00 am on 1 Nov 2010
$subject = 'Test: ' . date('g:i a \o\n j M Y');

$message = 'This email was automatically generated. Please send an email to yourusername@youremailprovider.com if you would like to disable these automated tests.';

$result = mail(TO_NAME . ' <' . TO_EMAIL . '>', $subject, $message, 'From: ' . FROM_NAME . ' <' . FROM_EMAIL . '>');
var_dump($result)

【问题讨论】:

  • 在您的 cron 文件中触发对会员资格即将到期的客户的查询,然后根据该查询更新您的查询
  • @RakeshShetty 谢谢,这是我第一次遇到 cron 工作,请您提供示例 cron 文件并解释我如何解决这个问题。
  • @Ramya 你说过你已经为此创建了一个 cron 文件在你的问题中添加代码
  • @RakeshShetty 我从网站上得到了这个,我在服务器上每五分钟就有一次 cron 作业,它每 5 分钟调用一次上述脚本并触发邮件。
  • 但是您还没有编写任何代码来从客户表中获取结果?您将比较该表中的到期日期

标签: php email cron


【解决方案1】:

假设您通过 cron 作业调用文件 sendmail.php,那么您的代码应该包含: (因为您没有提供任何表结构)

<?php

//get the result from clients table


//check whether clients expiry date is expired or not


// if yes then

//then said mail

//else

//do nothing


?>

【讨论】:

  • 所以这个脚本必须每分钟运行一次??
  • @Ramya 此脚本应该运行取决于您在 cron 作业中设置的时间
猜你喜欢
  • 2014-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-20
  • 1970-01-01
  • 1970-01-01
  • 2013-06-09
  • 1970-01-01
相关资源
最近更新 更多