Jerielle
Jerielle

Reputation: 7520

How to make a PHP script that can loop and run once in a week using CRON?

I need some help with this because I am still new in doing CRON process. I have a lot of emails I think 1052 emails. My plan I want to send twice a week and I will set Tuesday and Thursday. So that I can make an interval because I think it will stress the server process. Just correct me if I'm wrong. So 70 email per day so it will be 10 email per hour. Here's my table structure.

+-----------+--------------------------+------+-----+---------+----------------+
| Field     | Type                     | Null | Key | Default | Extra          |
+-----------+--------------------------+------+-----+---------+----------------+
| id        | int(4) unsigned zerofill | NO   | PRI | NULL    | auto_increment |
| client_id | int(10) unsigned         | NO   |     | NULL    |                |
| email     | varchar(100)             | NO   |     | NULL    |                |
| sent      | tinyint(1) unsigned      | YES  |     | 0       |                |
| sent_date | date                     | YES  |     | NULL    |                |
| excluded  | tinyint(1) unsigned      | YES  |     | 0       |                |
+-----------+--------------------------+------+-----+---------+----------------+

In default the sent column will change to 1 (the default is 0) if a certain email is sent. And after completing sending the email all of the email sent column will be back to 0 again. In that process I don't have a question.

My problem is how can I make a PHP script that can loop every hour and it will run on these following days (Tuesday and Thursday). In my Plesk panel I set up a simple cron that will run daily.

I am using CodeIgniter for this. In sending email I have a code but it will send all of the client. I just want to send 10 email per hour.

public function sendEmail() {

    $subj = $this->input->post('email_subject');
    $from = '[email protected]';
    $cc = $this->input->post('email_cc');
    $data['subject'] = $subj;
    $content = $this->input->post('email_body');
    $this->load->library('email');

    $config = array(
        'protocol'  =>  'sendmail',
        'smtp_host' =>  'mail.sample.ph',
        'smtp_port' =>  587,
        'mailtype'  =>  'html', 
        'charset'   =>  'utf-8'
    );

    $this->email->initialize($config);

    $email_list = $this->getClientEmails(); //all email for now
    $valid_email = array();
    $invalid_email = array();

    if(count($email_list) > 0) {
        for($x = 0; $x < count($email_list); $x++) {
            if(valid_email($email_list[$x]['email'])) {
                $valid_email[]  = $email_list[$x]['email'];
            } 
            //get all invalid emails
            /*else {
                $invalid_email[] = array(
                    'id'    =>  $email_list[$x]['id'],
                    'email' =>  $email_list[$x]['email']
                );
            }*/
        }
    }

    $email_string = implode(',', $valid_email);

    fd($email_list);

    $this->email->set_mailtype("html");

    $this->email->from($from, 'SAMPLEAdmin');
    $this->email->to($email_string); //get all emails from client
    $this->email->cc('[email protected]');
    $this->email->subject($subj);
    $this->email->message($content);

    $send_mail = $this->email->send();

    if($send_mail) {
        fp('success');
    } else {
        fp('failed');
    }   

    echo $this->email->print_debugger();

}

That's all guys I hope you understand what I really want. Just inform me if there are some misunderstanding.

Thanks.

Upvotes: 1

Views: 344

Answers (1)

Eddimull
Eddimull

Reputation: 342

First off, 1000 emails isn't a huge overhead. However, I like to go by the rule of minimizing as much server stress as possible, which you're doing.

PHP can't execute itself on a schedule. What Laravel does to get by this with its task scheduler is have a cron task kick off every minute and then the PHP code decides when to do what. So unless you are using a scheduler in a similar fashion you're going to need to execute your script at the day and times you want with plesk.

The actual task should look something like this

php -q directoryToYourFile/emailTask.php

The -q suppresses HTTP header output. Without the -q it'll email you whenever it runs. The actual file shouldn't be accessible from the internet because you don't want a web crawler to hit your file on accident.

**edit For what it's worth, I put all my scheduled php scripts outside the httpdocs directory and into a folder on the same level called tasks. That way, there's nothing that can hit the file except for you on command line or the server.

Upvotes: 3

Related Questions