Tuesday, 17 May 2016

How to set automatic email reminder using Cron Job?

Hello friends, today I am going to tell you how to set an email reminder or email notification. This can be done via Cron Jobs. Cron Jobs is helpful for reminder purposes or if you don't want to send same email every week or every single day then Cron Jobs is the best approach.  Cron Jobs can be set on the server or even on your own local system (Linux/Ubuntu). If you have Ubuntu or Linux then you can set Cron Jobs in your local system. Lets understand what is Cron Jobs first.
Cron Job is a way through which you can set and schedule routine background job at specific period of time. Now let us understand the syntax of how to set Cron:

  1. MIN HOUR DOM MON DOW CMD

MIN: First argument passed is Minute, at what minute do you want to set Cron to run. Value for minute ranges from 0 to 59.
Hour: Second argument passed is Hour, at what hour do you want to execute Cron. Value for Hour ranges from 0 to 23.
DOM: Third argument is DOM, this is day of month. Example: If you want cron to run every 29th day of the month then you can set DOM to 29. Value for DOM ranges from 1 to 31.
MON: Fourth argument is MON, this is month field. If you want to run the cron on June, then set MON to 6. Value for MON ranges from 1 to 12.
DOW: Fifth argument is DOW, this is day of week. You can set DOW to numeric from 0 to 6 or you can also set DOW to name of the day . For example: If you want to run the cron on sunday, then you can set DOW to "sun".
CMD: Last argument to be passed on cron is command. Here you will write the command that will be executed when cron will run.

Now lets write the code for sending email. Create file email.php

  1. <?php
  2. // the message wishing birthday to your friend
  3. $msg = "Wish you a very happy birthday....";
  4. // use wordwrap() if lines are longer than 100 characters
  5. $msg = wordwrap($msg,100);
  6. // send email
  7. mail("myfriend@example.com","Happy Birthday Subject",$msg);
  8. ?>

Now let us set an email notification or reminder for specific time say 29th May 7:00 AM:

  1. 0 07 29 05 * /home/amuk/email.php

0 - 0 Minute
07- 7 AM
28- 29th Day
05- 5th Month (May)
* - Every day of week
/home/amuk/email.php - Location of the file where your file is saved.

This is how you can schedule and set multiple cron jobs. Hope this blog helps you to create a cron job in Linux/Ubuntu. If you have purchased hosting then you can also set cron jobs by selecting cron jobs in cpanel and setting the command same way.

Thanks for reading the blog.
- See more at: http://findnerd.com/list/view/How-to-set-automatic-email-reminder-using-Cron-Job/19044/#sthash.V08ICw5k.dpuf

No comments: