Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Email members via Cronjob


Go to End


5 Posts   1188 Views

Avatar
luuk

Community Member, 15 Posts

8 December 2014 at 11:31pm

Hi Guys,

i'm implementing a feature for a REST webapp. I would like to send an email to all the Members of a certain group based on some logic. This has to happen every day so i need cronjobs. It is an app for an hospital where people with OCD can practise their exercises at home. Based on their therapy they need to do a certain amount of exercises a week. So i would like to send them email reminders. i can hardly find any examples so i hope you guys could help me. Please let me know if this is the right way to do this:

here's my EmailTask.

class EmailTask extends BuildTask {
 
    protected $title = 'Email patients';
    protected $description = 'A task that emails patients exercises';
    protected $enabled = true;
 
    function run($request) {
        Patient_Controller::init();
    }
}

Here's the controller i'am calling:

class Patient_Controller extends Controller {
	private static $allowed_actions = array('email');
	
	public function email() {
		$email = new Email();
		$email->setTo($this->Email); 
	    $email->setSubject('test');
	    $email->setFrom('no_reply@mysite.com'); 
	    $email->setTemplate('EmailReminder');
		$email->send();
	}
}

i keep getting this error: [Strict Notice] Non-static method Patient_Controller::email() should not be called statically, assuming $this from incompatible context.

The other question is, do i need to put the logic in my buildtask (looping over al the patients and send them their email) or is this the right way to do it?

Avatar
WandL

Community Member, 9 Posts

9 December 2014 at 1:13pm

Hi Luuk,

To get rid of the error just declare your email function static:

public static function email() {
...

Avatar
Mo

Community Member, 541 Posts

10 January 2015 at 3:20am

Hi Luuk,

You shouldn't need to add your email logic to a controller, you should be able to run the whole thing in the build task itself. Is there a particular reason you are using the controller outside of your question?

I would probably just add the Member::get() loop to the build task, then you should be able to add the email send code inside that loop, without calling a controller.

Just bear in mind that if your loop takes longer than a minute and you have set the cron to run every minuite then you might get some bad side effects (multiple overlapping DB connections for example).

Also, have you looked at the crontask module? I have used it a few times, it is quite nice: https://github.com/silverstripe-labs/silverstripe-crontask

Cheers,

Mo

Avatar
Mo

Community Member, 541 Posts

10 January 2015 at 3:38am

Edited: 10/01/2015 3:39am

Just thought what I was saying might be clearer if I wrote the code down. As far as I am aware, if you are just sending the email using cron (via your build task) and the controller isn't needed for anything else, you should be able to use:

class EmailTask extends BuildTask {
 
    protected $title = 'Email patients';
    protected $description = 'A task that emails patients exercises';
    protected $enabled = true;
 
    function run($request) {
        $members = Member::get();

        foreach($members as $member) {
            $email = new Email();
            $email->setTo($member->Email); 
            $email->setSubject('test');
            $email->setFrom('no_reply@mysite.com'); 
            $email->setTemplate('EmailReminder');
            $email->send();

            // Use this if you want to log that the message was sent
            echo "<p>Email sent to {$member->Email}</p>";
        }
    }
}

Hope that makes sense?

Mo

Avatar
luuk

Community Member, 15 Posts

10 January 2015 at 3:47am

Thank you very much, i will test this asap!