CodeIgniter Forums

Full Version: What to use to send an email atomatically at certain time of day
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there,

I am looking to do a bit of testing with codeigniter with sending an automated email to a specific account without me on the system.  So what I would love it to do is say at 1pm every day it kicks off s script to create and send an email.  I know how to create the email part, I am just trying to find out what I should be using to get it to automatically send it at 1pm, can I do this in php or would I have to look at jquery or something else like that?

Thanks,

Doomie
I think you need to set up a cron jobs from the cPanel to send automatic emails.
Is cron jobs the only way of doing it as I will end up with a lot of sites that will run same script?
Since the code for the site only runs when someone accesses a page on the site, you can't really trigger something at a specific time unless you schedule it with the OS (which is, more or less, what a cron job is).

There are a few good tutorials out there about how to setup cron jobs from PHP, but this does depend on you having access to actually do that on your server. You could probably find a good PHP library to do it, as well. If you wanted to schedule/configure cron jobs from your CI website (you would definitely want to make sure that access to the page is locked down), you would need to adapt the tutorial/library to CodeIgniter, but the difficulty of that is largely dependent on your specific requirements.

If you have shell access on the sites, it would probably be easier to write a shell script to setup the cron jobs.
OK I have been trying to dabble for a while now and I am at the stage that if I run it myself it works fine and does what I want it, if I try to cron job it, it errors as it doesn't have all the information.

PHP Code:
public function index(){
 
           
            $this
->load->dbutil();

 
           $backup $this->dbutil->backup(); 

 
           $this->load->helper('file');
 
           $subject 'backup-'.date("d-m-Y").' from Site Name';
 
           $file_name 'backup-'.date("d-m-Y").'.gz';
 
           write_file($file_name$backup); 
 
           $this->email->from('[email protected]''Backup System');
 
           $this->email->to('[email protected]');
 
           $this->email->subject($subject);
 
           $all 'Filename: '.$file_name."<br>";
 
           $this->email->message($all);
 
           $this->email->attach($file_name);
 
           $this->email->send(); 
 
           delete_files('backup/');


 
       

I have removed the emails from this code.  When I get cron to run it, I get an error right at the start with the CI_controller as I know that it doesn't know what it is as its not ran everything else in the browser first.  All this is doing is grabbing the database and packing it into a file, sending it to an email address and then deleting the file in the folder (not that bothered about the deleting part atm).  Would I have to look into my own CI_controller, if so any tips on what I should be trying to make in there?