Jump to content

Cron Job for ProcessWire mailer template


BFD Calendar
 Share

Recommended Posts

I have a template that includes a php mail script sending the results of an array. It works fine when I load the page in a browser, it sends the page content like it should.

I want this to happen every day at 1.00 am, so I have a Cron Job (Tasks Scheduler on my server) scheduled to perform the script.

Command executed  : /usr/local/bin/php.ORIG.5_3 -c /usr/local/lib/php.ini-2 /homez.183/birthfac/www/site/templates/bfd_mailer.php

But I get a PHP Fatal error:  Call to a member function find() on a non-object on line 51:

$features = $pages->find("parent=/events/, bfd_day.name=$todayday, bfd_month.name=$todaymonth, sort=bfd_year");

So I presume the doesn't understand the ProcessWire code "parent=/events/"....

Any suggestions for a workaround or solution?

Link to comment
Share on other sites

It doesn't know"$pages". Is the mail script bootstrapped on PW or are you calling a template page directly from the cron job? You might try using wire('pages'), I am not sure this will work, depending on how that script is set up. I think the most reliable and easiest way would be to use the LazyCron PW module: http://modules.processwire.com/modules/lazy-cron/

Link to comment
Share on other sites

Horst, the 'tasks scheduler' only allows me to enter the url of a script/page on the server and fix a time when it should be executed. I can't edit the cronjob itself (or I haven't figured out where to do it).

Adrian, 'bfd_mailer.php' is the template that has the mail script. Load it in a browser and the content is sent. 'wire('pages')' didn't do the trick. LazyCron looks promising, but I see Captain Hook luring around the corner again. Still gobbledegook to me but I guess I'll have put my teeth in there sooner or later. I kinda get the idea behind it but applying it is something else....

Basically the hook says 'myFunc' is sending the email (script in template) 'everyDay'? Or is 'myFunc' finding the pages AND sending the email? And it should be not just 'everyDay' but every day at 1.00 am.

So should I distill it all from http://processwire.com/api/hooks/ and http://processwire.com/api/hooks/captain-hook/ or are there more practical examples out there?

Anyway, thanks until now.

Link to comment
Share on other sites

Horst, the 'tasks scheduler' only allows me to enter the url of a script/page on the server and fix a time when it should be executed. I can't edit the cronjob itself (or I haven't figured out where to do it).

Ah, ok. You may also try to create a simple php script somewhere outside of pw with this content:

<?php
header("location: http://www.your-site.com/page-url-with-mail-script/");

or with this one:

<?php
file_get_contents("http://www.your-site.com/page-url-with-mail-script/");

and call that with your cronjob.

  • Like 2
Link to comment
Share on other sites

But I get a PHP Fatal error:  Call to a member function find() on a non-object on line 51:

$features = $pages->find("parent=/events/, bfd_day.name=$todayday, bfd_month.name=$todaymonth, sort=bfd_year");
So I presume the doesn't understand the ProcessWire code "parent=/events/"....

That error is caused by the fact that you run this file without involving ProcessWire at all. You can't just run template file "standalone" and expect it to automatically load ProcessWire, you know :)

Solution @horst provided above is most likely what you need here (or external / real cron job instead of this awfully limited scheduler thing you're using now).

Then again, if this is the only reason you need that template, i.e. it doesn't need to be web-accessible, you could forget whole template and page thing and simply place a PHP file somewhere your scheduler can access (such as your PW root directory) and in that file bootstrap PW and write any program logic you need. Point your scheduler to that file and you're all set.

  • Like 2
Link to comment
Share on other sites

What I mostly do with cronjobs is to create a page inside /processwire/ and give guest acces to that page template. 

That template is only used for that 'cron' page. So after the page is created, disallow other pages using that template.

Say your page with guest access set and is named cron,

your cron job can visit yourdomain.ext/processwire/cron/.

Link to comment
Share on other sites

Thanks all. Horst's solution did the trick. I tried

<?php header("location: http://www.your-site.com/page-url-with-mail-script/");

before but it didn't work.

<?php file_get_contents("http://www.your-site.com/page-url-with-mail-script/");

however worked.

Teppo, the page I'm calling is indeed hidden because I'm using it only to send my daily array to Blogger. As soon as it runs smooth Blogger is configured to accept and post automatically. And of course the page shouldn't be reachable otherwise.

Link to comment
Share on other sites

Teppo, the page I'm calling is indeed hidden because I'm using it only to send my daily array to Blogger. As soon as it runs smooth Blogger is configured to accept and post automatically. And of course the page shouldn't be reachable otherwise.

Glad to hear that you found a solution, but in a case like this I'd really suggest taking a look at bootstrap method. It's perfect for tasks like these, where you don't really want or need actual page, just code that can access data and/or features of ProcessWire.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...