Send email daily from WordPress site

I customized a calendar plugin which shows today’s birthdays and current month’s list of wedding anniversaries in the home page of the site. I wrote a code in that plugin’s displaying page using wp_mail and mail will send. But this happens only when the site is visited. My code:

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>' . "rn".'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";
    }
  }
 }

I heard about wp_cron but when I searched in this forum about how to write cron in WordPress I saw an answer like

Read More

Unfortunately the WordPress cron jobs are only triggered when your site is visited

If it is true then how can I send emails daily even without visiting the webpage? Is there any other way for this?

Related posts

Leave a Reply

1 comment

  1. Unfortunately the WordPress cron jobs are only triggered when your site is visited

    This is true.

    The only way to reliably execute code (even if visitors don’t visit your site) is to use the unix cron functions.

    You can just setup a unix cron job that will do a wget on your homepage.

    Edit:

    You can read this article to figure out how to setup unix cron job.

    Just setup the usual wp_cron and schedule it to run at what ever time you want to run it.

    After that setup a unix cron job to do a wget on your homepage. This will trigger a page view which in turn will trigger the wp_cron job.