0

We are working on automatic mail now. I am trying to implement automatic mail using cron. How do I call cron from the controller??

exec("0 * * *, php installation path php, public_html/index.php SendMail autoMail")

I called it because I was told to call it like this, but it doesn't work.

I want to control automatic mail by creating, modifying, and deleting cron in the controller.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Not sure where you were told to call `cron` like that, but please don't. Instead make use of `cron` commands on the server itself (or cPanel, if you're using shared hosting). – Obsidian Age Apr 05 '22 at 01:58
  • `exec()` is for executing shell commands. That needs to be put in the user's crontab with the `crontab` command. Also, you have an extra `,` and you're missing one of the schedule fields. – Barmar Apr 05 '22 at 02:01
  • exec("0 0 * * * /php path/php -q /codeigniterdirectory/index.php controllerName methodName"); I got the answer that I can call it from the controller like this. – HaeRyong Jeong Apr 05 '22 at 04:06
  • Wherever you got that answer, it is wrong. That is not a command, it is the format of a crontab file, which is something like a config file for running things at specific times. `cron` is a server/OS utility, not something you "run" or call from CI. You can set up `cron` to run one of your CI CLI processes though, and there are many examples of how to do that here on SO, eg https://stackoverflow.com/questions/19269566/how-to-set-cron-job-url-for-codeigniter; here's info about how to create a cronjob https://stackoverflow.com/a/878647/6089612 – Don't Panic Apr 05 '22 at 05:24
  • Does this answer your question? [How to set cron job url for codeigniter?](https://stackoverflow.com/questions/19269566/how-to-set-cron-job-url-for-codeigniter) – Don't Panic Apr 05 '22 at 05:26
  • The cron must be created, modified, and deleted in the controller. – HaeRyong Jeong Apr 05 '22 at 07:08

1 Answers1

0

You can try with the below code:

Controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
class Cronjob extends CI_Controller {
static $model     = array();
static $helper    = array();
public function __construct(){
    parent::__construct();
}
public function sendMail()
{
$msg = "test";
$msg = wordwrap($msg,70);
// send email
mail("youemail@gmail.com","yoursubject",$msg);
}}

AND Then Search cronjob in cpanel then select job and write in command

/usr/local/bin/php /Home Directory/public_html/index.php controllerName/funcionName

EXAMPLE: /usr/local/bin/php /home3/faizuldev/public_html/index.php cronjob/sendMail

click : show screenshot