7

My website hosting server is hostmonster.com.

My application uses codeigniter framework.

I have a code which sends emails to my users and I want to make it automatic.

I have used the cpanel of the hosting service and I tried to give the command as

php -q www.mysite.com/admin admin sendDailyEmail

my controller is admin and the method is sendDailyEmail and the controller is present inside the application/controllers/admin folder.

I have also set a reminder email to me whenever the cronjob is run.

The email subject reads

Cron php -q /home1/username/public_html/admin admin sendDailyEmail

and the body says

No input file specified

Where do I go wrong.

I have never run cronjobs and this is my first time. I am no good in giving command line instuctions too.

My admin sendDailyEmail code is as follows

function sendDailyEmail() {
    $data = $this->admin_model->getDailyData();
    foreach ($data as $u) {
    if($u->Daily){
     //if(!$u->Amount){
       if ($u->Email=='myemail@gmail.com') {

                $user['user_data']['FirstName'] = $u->FirstName;
                $user['user_data']['LastName'] = $u->LastName;
                $user['user_data']['Id']=$u->Id;

                $this->email->clear();
                $this->email->to($u->Email);
                $this->email->from('alerts@mysite.com', 'MySite');
                $this->email->subject("My Subject");
                $msg = $this->load->view('emails/daily_view', $user, true);
                $this->email->message($msg);
                if ($this->email->send())
                    $data['message'] = "Daily Emails has been sent successfully";
                else
                    $data['message'] = "Daily Emails Sending Failed";
            }
        }
    }
    $data['main_content']['next_view'] = 'admin_home_view';
    $this->load->view('includes/admin_template', $data);
}
spod
  • 406
  • 3
  • 5
  • 19
  • I have removed the authentication but if i now copy paste the url, it just sends away an email to my users. I fear that if someone knows, they keep on sending emails just by using the url. How do I restrict this. I am using codeigniter. The cronjob is run through the cpanel. previously i have this code in my sendDailyEmail if (!$this->session->userdata('variable')) redirect('admin/admin', 'refresh'); – spod Dec 26 '13 at 06:07

5 Answers5

16

You can use wget and set the time for whatever you like:

wget http://www.mysite.com/admin/sendDailyEmail

You can also use curl:

curl --silent http://www.mysite.com/admin/sendDailyEmail
doitlikejustin
  • 6,293
  • 2
  • 40
  • 68
  • The cronjob is done and I got an email saying ** --2013-10-05 11:20:01-- http://www.mysite.com/admin/admin/sendDailyEmail Resolving www.mysite.com... 66.147.240.184 Connecting to www.mysite.com|66.147.240.184|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 0 [text/html] Saving to: `sendDailyEmail' 0K 0.00 =0s 2013-10-05 11:20:11 (0.00 B/s) - `sendDailyEmail' saved [0/0] ** But it did not send out the email – spod Oct 05 '13 at 17:32
  • If you type `http://www.mysite.com/admin/sendDailyEmail` directly in the browser does it send the email? – doitlikejustin Oct 05 '13 at 18:43
  • Tried this in my shared hosting env and got "Permission Denied" for wget and "request has been blocked by Mod Security" for curl. – mpemburn Dec 03 '13 at 13:12
12

For CodeIgniter 2.2.0

You can try this:

 php-cli /home/username/public_html/index.php controller method

or at your case

 php-cli /home/username/public_html/index.php admin sendDailyEmail 

It works fine with me.. Cheers!

JiNexus
  • 2,754
  • 1
  • 19
  • 17
3

Codeigniter sets up command line differently for running crons, etc.

Read: http://www.codeigniter.com/user_guide/general/cli.html

So you should run:

php index.php admin admin sendDailyEmail

(that may need adjusted; based on your code above)

Have a look at an article I just wrote that goes a little deeper into it all:

http://codebyjeff.com/blog/2013/10/setting-environment-vars-for-codeigniter-commandline

Draken
  • 3,134
  • 13
  • 34
  • 54
jmadsen
  • 3,635
  • 2
  • 33
  • 49
0

i have facing same issue while, but following work for me

wget http://www.yoursite.com/controller/function
Ravi Mane
  • 1,468
  • 2
  • 18
  • 24
0

Here's my example for the cron job on cpanel using Codeigniter 3 framework:

/usr/local/bin/php /home/user/public_html/project/index.php controller method

Credits to Shinerweb youtube channel https://www.youtube.com/watch?v=Pgc2vzrlwok

drmsh
  • 1
  • 1