1

as suggested I created a File MessengerCommand.php under protected/commands as

class MessengerCommand extends CConsoleCommand
{
    public function run($args)
    {
        /* if(ERunActions::runBackground())
        { */

       $mail=Yii::app()->Smtpmail;
        $mail->SetFrom("tsadmin@softthink.com", 'From NAme');
        $mail->Subject    ="hello";
        $mail->MsgHTML("haiii workd");
        $mail->AddAddress("rajesh.udutha@itaugments.com", "");
        if(!$mail->Send()) {
            echo "Mailer Error: " . $mail->ErrorInfo;
        }else {
            echo "Message sent!";
        }
}
}

and added yiic command as

$path = dirname(__FILE__);
//echo $path;
shell_exec( $path . "/protected/yiic messenger" );

and it will trigger email when I load the site ....

but I dont wanna refresh the site ..I need to make this to run in background ..Please help me.

rch
  • 345
  • 1
  • 6
  • 20

2 Answers2

1

The windows equivalent to a cron job is a scheduled task.

A scheduled task can be created using command line with schtasks

An example:

schtasks /create /tn calculate /tr calc /sc weekly /d MON /st 06:05 /ru "System"
Samay
  • 465
  • 6
  • 19
  • ha but i had a website in yii..In that site ,how to set remainder email for particular date and trigger email on that date without any frontend process and with only backgroundprocess in the site....in yii – rch Nov 10 '16 at 08:39
  • You may do that by calling the local URL using Wget or CURL. You may download [Wget for Windows](http://gnuwin32.sourceforge.net/packages/wget.htm) – Samay Nov 10 '16 at 08:44
  • i wanna send remainder email from mysite without loading site ,i want it to run background without any action from frontend of my site.. – rch Nov 11 '16 at 04:36
  • @CRajesh - WGET shall exactly do the same. e.g `wget http://localhost/projectname/sendmail/` – Samay Nov 11 '16 at 07:49
  • schtasks is a command used to schedule tasks which is similar to that of cron job. You may refer to its usage page [here](https://support.microsoft.com/en-us/kb/814596) – Samay Nov 30 '16 at 17:10
  • thank you @Samay and finally I have noticed that My cpanel has option of cronjobs..and i made as show [here](https://kayako.atlassian.net/wiki/display/DOCS/Setting+up+a+server+cron+or+scheduled+task) – rch Dec 01 '16 at 03:47
1

You can use yii console applications to accomplish your task.

In protected/commands create a new file with Command sufix, for example: MessengerCommand.php:

<?php
class MessengerCommand extends CConsoleCommand
{
.......

In class MessengerCommand you have several options to create the command action. In this sample we will override run method:

public function run($args)
{
        $birth_month = date("m");
        $birth_day = date("d");
        $criteria = new CDbCriteria;
        $criteria->condition = "birth_month = $birth_month and birth_day = $birth_day";
        $listScheduledRecords = Table::model()->findAll($criteria);
        foreach($listScheduledRecords as $scheduled_record):
            $this->send($scheduled_record);
        endforeach;
}

public function send($scheduled_record)
{
    ....
    your logic to send your email
    ....
}

In protected dir, create a file: messenger.php. This file will be the command executer:

<?php
$path = dirname(__FILE__);
$output = shell_exec( $path . "/./yiic messenger" );
echo $output;

To test it, on Linux/Unix, run in console/terminal:

cd /.../.../...your_protected_path 
php messenger.php

To test on Windows, you need to refer to your php.exe location path or set php.exe on your system environment variables and use yiic equivalence for Windows

To schedule automatic task, in this sample, daily execution, on Linux/Unix, you can use cron jobs:

In console/terminal:

crontab -e

In cron file, add the scheduled task, daily, at 9h00. Remember cron sintax: # minute hour day of month month day of week command

0   9  *  *  * php /full_path/protected/messenger.php

Save file and exit.

To schedule automatic task on Windows, refer to their docs / help on Internet.

If you have errors, Yii Console applications use their own config file (protected/config/console.php). Common mistakes are wrong db connection, components, modules in protected/config/console.php.

Alejandro Quiroz
  • 2,604
  • 1
  • 15
  • 17
  • it is impressive and I will try and give you back feedback – rch Nov 22 '16 at 03:41
  • Thank you for ur support – rch Nov 22 '16 at 03:47
  • I did it ,,but mail is sending when i refresh the website ...the process is not running under background...please help at the last stage...of problemmmmm – rch Nov 29 '16 at 12:14
  • @CRajesh please share source code. Edit your question with all the code you are using – Alejandro Quiroz Nov 29 '16 at 13:54
  • @CRajesh do you setup scheduled job? What operating system are you using? What results you get testing command, for example php messenger.php ? – Alejandro Quiroz Nov 30 '16 at 04:48
  • I am using Windows 7 ..and I wanna send emails if today is birthdate...and i want it to run background..but above my example....sends email if i refresh the site .then checks the database and sends mail...i want it to run background... – rch Nov 30 '16 at 05:27
  • Do you schedule command execution? in Windows you must to use scheduled tasks – Alejandro Quiroz Nov 30 '16 at 05:30
  • Command doesn't need to run in browser. Make sure to build command properly : "php.exe C:\...........\protected\messenger.php", test it on cmd.exe before create scheduled task – Alejandro Quiroz Nov 30 '16 at 05:34
  • Dont know ..how to attach these program to scheduled tasks ?..does it work on online server... – rch Nov 30 '16 at 05:35
  • I don't use Windows, hope it helps you to schedule a command in Win task scheduler http://stackoverflow.com/questions/28507707/how-to-launch-cmd-exe-to-execute-command-using-task-scheduler you are too close to solve your question – Alejandro Quiroz Nov 30 '16 at 05:41
  • haa...but don't know how to attack these schedule to my site – rch Nov 30 '16 at 06:50
  • Your site is on hosting? Own network? Windows? Linux? In your machine? – Alejandro Quiroz Nov 30 '16 at 06:52
  • Have you tested on your local development environment this scheduled task before to try on your server? – Alejandro Quiroz Nov 30 '16 at 07:23
  • You need to connect to your Windows server, maybe with terminal server, remote desktop and configure task scheduler with the command or Ask for help with your hosting provider. Sorry, I don't use Windows and can't help you with that environment – Alejandro Quiroz Nov 30 '16 at 07:26
  • You need to schedule your new yii command on your Windows server task scheduler : "C:\... your php path ...\php.exe C:\...your yii protected path...\messenger.php" – Alejandro Quiroz Nov 30 '16 at 07:35