每天从wordpress网站发送电子邮件

时间:2014-04-09 09:14:06

标签: php wordpress email cron

我定制了一个日历插件,在该网站的主页上显示今天的生日和当前月份的结婚纪念日列表。我使用wp_mail在plguin的显示页面中编写了一个代码,邮件将发送。但这只有在访问网站时才会发生。我的代码:

if($dat==date('Y-m-d'))/*$dat is the date of event from DB*/
 {
  if($eid!=''){ /*if recipient email id is not null*/
 if($se!=1)  /*if email is sending first time then($se=db column 'send'value) $se=0    otherwise it is 1*/
  {
    $to=$eid;
    $sub="Birthday Wishes";
    $msg='Happy Birthday '.$ev_title[$j];
    $headers= 'From:Mysite <noreply@mysite.com>' . "\r\n".'Content-type: text/html'; 
    $attachments=array(WP_CONTENT_DIR . '/plugins/spider-event-calendar/images/happybday.gif');
    $rx=wp_mail($to,$sub,$msg,$headers,$attachments);
    $wpdb->update($wpdb->prefix.  "spidercalendar_event",array('send'=>1),array('id'=>$ev_id[$j]));/**/
    //echo "email send";
    }
  else{
  //echo "email already sent";
      }
    }
  }

我听说过wp_cron但是当我在这个论坛中搜索如何在wordpress中编写cron时,我看到了一个像

这样的答案

Unfortunately the WordPress cron jobs are only triggered when your site is visited 如果确实如此,那么即使不访问网页,我如何每天发送电子邮件。还有其他任何方式吗?

1 个答案:

答案 0 :(得分:0)

登录后,您可以使用终端通过以下过程设置常规cron作业;

使用;

访问您的con作业列表

crontab -e

可能会有一些线路。输入 o 以启用文件编辑;

您可以使用以下网站生成命令,以便发出http://www.corntab.com/pages/crontab-gui

作为我在服务器上的示例作业;

*/5 * * * * /usr/bin/php -q /var/www/www.mysite.com/cron.php >/dev/null 2>&1

此作业每5分钟运行一次php脚本,并阻止网站管理员每次运行时都会收到一封电子邮件。这就是2>&1在行尾的作用。

我实际上有点不确定/usr/bin/php -q>/dev/null做了什么,因为我刚刚从另一个已经以这种方式设置的工作中复制它们。我通过命令行对此很新。

完成编辑后,按 esc ,然后按 Shift + zz 退出。你应该完成。

我建议您使用自行发送电子邮件的测试脚本,以确保它首先正常工作,然后将其更改为使用您尝试运行的脚本。

我不知道这个过程对于不同的服务器设置是否会有所不同,但它可能是。这就是我必须为使用apache运行CentOS的服务器所做的事情。

相关问题