Cron Jobs

Cron job is a utility or scheduled task which run command, script or program on your server automatically without any user interaction at specific time and date. Basically it is a background process runs on server which perform some task without knowing to user. For example, sending mail to user on particular time and date. Most importantly it runs automatically, but once we need to configured it's running date and time. It can be also called Scheduled Task.

This blog will help you to learn how to send email in background with the help of PHPMailer and Cron Jobs. Also show you how to run .php page from Task Scheduler.

Create php file to send mail

Step 1: First of all you have to start XAMPP or WAMP and in xampp you have to start Apache and MySQL. After that create one folder into C:\xampp\htdocs\ and give appropriate name to that folder. I created folder name CronJobs. In that folder create one file with .php extension i.e index.php, and write code in index.php file which you want to execute when Cron Job run.

index.php

<?php

    $email = "ToEmailAddress@xxx.com";  /* Enter email adddres whom you want to send mail */

 

    require 'phpmail/PHPMailerAutoload.php';

    require 'phpmail/class.phpmailer.php';

    include 'phpmail/class.smtp.php';

  

    $mail = new PHPMailer;

    $mail->isSMTP();

    $mail->SMTPDebug = 0;

    $mail->Debugoutput = 'html';

    $mail->Host = 'smtp.gmail.com';

    $mail->Port = 587;

    $mail->SMTPSecure = 'tls';

    $mail->SMTPAuth = true;

    $mail->Username = "FromEmailAddress@xxx.com"; /* Enter sender emailid of gmail.*/

    $mail->Password = "FromEmailAddress Password"; /* Enter sender password of gmail.*/

    $mail->setFrom('FromEmailAddress@xxx.com');

    $mail->addAddress($email);

    $mail->Subject = "Cron Jobs";

    $mail->Body = 'Demo of cron jobs..';

    $mail->send();

?>

Create Cron Job

Step 1: Create batch file named "script.bat" and put the following code in that file. I created file in C:\xampp\htdocs\CronJobs folder.

"C:\xampp\php\php.exe" –f  "C:\xampp\htdocs\CronJobs\index.php"

Note: First is a path for php.exe file. This file located into C:\XAMPP\php\php.exe and in second you have to enter path of the file which you want to execute in Cron Job C:\xampp\htdocs\CronJobs\index.php.

Step 2: Create vb script named "shell.vbs" and put the following code in that file. I created file in C:\xampp\htdocs\CronJobs folder.

Set WinScriptHost = CreateObject("WScript.Shell")

WinScriptHost.Run Chr(34) & " C:\xampp\htdocs\CronJobs\script.bat" & Chr(34), 0

Set WinScriptHost = Nothing

Now you have to set this Cron Job to run every 5 minutes. For that you have to start Task Scheduler.

Set Cron Job to Task Scheduler

Step 1: Start Menu->Task Scheduler. It will look like.

Step 2: Click on Action from menu bar and in that click on Create Task.

Step 3: Click on Create Task one window will open in that give appropriate Name and Description of task. Window will look like.

Step 4: After giving name and description click on Triggers tab. It will look like.

Step 5: Now click on New button, another window will appear. In that window you have to check Repeat task every checkbox. After that in first dropdown select 5 minutes and in second dropdown select Indefinitely (So the duration of 5-5 minutes the code will execute) and then click on OK button.

Step 6: After clicking on Ok button click on Actions tab. It will look like.

Step 7: Then you have to click on New button, one window will appear. In that window you have to set Program/script (C:\Windows\System32\wscript.exe) and in Add arguments(optional) you have to set the path of shell.vbs file in double quotes ("C:\xampp\htdocs\CronJobs\shell.vbs").Then click on OK button.

Step 8: After that all your work done click on OK button your task will look like. Mail will send to the receiver in every 5 minutes in background.

Hope you will like the blog. If you have any query comment below. Thank you..!!