This is exactly how my app works.
I can as an auth admin via the browser hit my controller action to
send out notifications.
The same Controller action can be called from the shell in interactive
mode in which it outputs info about what emails were sent.
The same Controller action can be called from the crontab where no
information is output this is controlled by a parameter passed to the
shell command.
My Controller function:
function notify(){
$this->layout = 'default';
$conditions = array('Profile.notify' => 1);
$recievers = $this->Profile->find('all', array('conditions' =>
$conditions));
if (!defined('FULL_BASE_URL')) {
define('FULL_BASE_URL',
Configure::read('Sandbox.site.url'));
}
foreach($recievers as $reciever){
if($reciever['Profile']['notified'] == null){
$reciever['Profile']['notified'] = date('Y-m-d H:i:s',
Time() - (1440*60*14));
}
$curTime = date('Y-m-d H:i:s', Time());
$vardump = $this->Project->findnew($reciever['Profile']
['notified']);
//$this->set('notifiee', $reciever);
//$this->set('email', $vardump);
if(!empty($vardump)){
$this->set('notifiee',$reciever);
$this->set('email', $vardump);
$this->Email->_debug = false;
$this->Email->reset();
$this->Email->charset = 'iso-8859-15';
$this->Email->to = $reciever['Profile']['email'];
$this->Email->subject = 'New Projects and
Attachments';
$this->Email->replyTo =
Configure::read('Sandbox.site.email');
$this->Email->from =
Configure::read('Sandbox.site.name').'<'.Configure::read('Sandbox.site.email').'>';
$this->Email->template = 'projectemail';
$this->Email->sendAs = 'html'; // because we
like to send pretty
mail
$this->Email->delivery = 'smtp';
if($this->Email->send()){
$this->Profile->id = $reciever['Profile']['id'];
$this->Profile->saveField('notified', $curTime);
}else{$this->log('Notify email could not be sent');}
}else{
//update the notified date.
$this->Profile->id = $reciever['Profile']['id'];
$this->Profile->saveField('notified', $curTime);
}
}
exit;
$this->layout = 'default';
$this->pageTitle = 'Sample Test Email';
}
Here is my vendors\shells\notify.php:
It simply loads and sets up the Controller and EmailComponent
And then calls the controller function listed above.
<?php
App::import('Core', array('Controller','Component',
'View','Model','Router'));
App::import('Component', 'Email');
App::import('Controller', array('Profiles','Projects'));
App::import('Model','Profile');
class NotifyShell extends Shell {
var $uses = array('Profile', 'User');
var $ProfileController = null;
var $Component = null;
var $Profile = null;
/**
* Controller class
*
* @var Controller
*/
var $Controller;
/**
* EmailComponent
*
* @var EmailComponent
*/
var $Email;
function initialize() {
$this->ProfileController =& new ProfilesController();
$this->ProfileController->uses = array('Profile','User');
$this->ProfileController->Profile =& new Profile();
$this->ProfileController->Project =& new Project();
$this->ProfileController->Email =& new EmailComponent();
$this->ProfileController->Email->initialize($this-
>ProfileController);
$this->_loadModels();
}
function main() {
$this->send();
}
function startup() {
$this->out('Cake notify sends emails of new Projects and
Attachments
'.date('Y-m-d',time()));
$this->hr();
}
function help() {
$this->out('CakePHP Notify:');
$this->hr();
$this->out('The Notify script sends emails to all profiles ');
$this->out('that are set to be notified.');
$this->out('The email contains all Projects and Attachments');
$this->out('which have been updated since the last time a
notification');
$this->out('Was sent.');
$this->out('This shell is intended to be called from a cron
job.');
$this->hr();
$this->out("Usage: cake notify send <interactive>...");
$this->hr();
$this->out('Commands:');
$this->out("\n\tnotify help\n\t\tshows this help message.");
$this->out("\n\tnotify send\n\t\t sends emails.");
$this->out("\n\tinteractive is passed as a 1 or 0. When set to 1
will echo status info.");
$this->out("");
print_r($this->interactive);
}
function send(){
if (isset($this->args[0])){
$this->interactive = $this->args[0];
}
$this->ProfileController->notify();
}
}
?>
On Aug 3, 3:17 pm, DigitalDude <[email protected]> wrote:
> Hey,
>
> that makes perfectly sense and I think I would prefer this one, too.
> There are two things I'm worried about:
>
> 1) I will need some sort of authentication to prevent calling those
> actions from direct access by url (sort
> ofwww.mydomain.com/news/generateStatusReport
> or else...). When called from the cronjob the action should only be
> permitted for the cron script
> 2) what could be the problem that I can call the action by url and
> emails get sent just fine and when I call the same action by cron
> script emails will not be sent...?
>
> Would be so nice if this could work...!
>
> On 3 Aug., 21:58, Johnathan Iannotti <[email protected]> wrote:
>
> > Why not put all of your logic within the model/controller and then call
> > that from the shell script/cron job you have going? That way you could
> > access those methods from the app and not just the shell script. Doesn't
> > make sense to separate that kind of logic..
>
> > -----Original Message-----
> > From: [email protected] [mailto:[email protected]] On
> > Behalf Of DigitalDude
> > Sent: Tuesday, August 03, 2010 2:13 PM
> > To: CakePHP
> > Subject: Sending StatusMails by CronJob - what is the right approach?
>
> > Hey,
>
> > I read uncountable articles now and tried really hard to get this to
> > work, but I just can't figure out how to solve this problem:
>
> > Let's say I want to write a cronjob that will check every sunday
> > what's new in a database and sends an overview of things that are
> > still to do to the users of my app.
>
> > I found several approaches to do that, but I can't figure out what's
> > the best way here.
>
> > I know how to call a shell script from a cron job and I already
> > managed to do this, I even have Access to a model and even controller
> > actions.
>
> > The other thing I have is a way to run actions directly from shell by
> > a dispatcher-script which I found in the bakery.
>
> > Both approaches have one big problem:
> > I CAN'T get them to send an email thorugh the Cake Email-Component!
>
> > No matter what I do, if the sending is within the called action by the
> > dispatcher call or if I try do import the Email-Component into the
> > Shell-script, it will never send me any email.
>
> > Additionally, I need to know one very important thing:
>
> > Does the logic for such status reports (or even newsletters or so)
> > belong into the SHELL script or is it common to call a specific action
> > in a specific controller to collect the data that is needed and send
> > the email?
>
> > It would be MUCH appreciated if somebody could even give me a hint or
> > maybe just an opinion to these questions...
>
> > Regards,
>
> > DD
>
> > Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp others
> > with their CakePHP related questions.
>
> > You received this message because you are subscribed to the Google Groups
> > "CakePHP" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected] For more options, visit this group
> > athttp://groups.google.com/group/cake-php?hl=en
>
> > Homepage:http://www.blueclover.com | Blog:http://www.blue-ings.com |
> > Twitter:http://twitter.com/bluecloverusa |
> > Facebook:http://www.facebook.com/blueclover |
> > YouTube:http://www.youtube.com/user/bluecloverstudios|Join Our Mailing
> > List:http://visitor.constantcontact.com/manage/optin?v=001ti8yLLtrqgo7unTQ...
>
> > ---------------------------------------------------------------------------------------------------------------------
>
> > This email transmission, including any attachments thereto, may contain
> > information that is proprietary and confidential. The information is
> > intended only for the use of the individual(s) or entity to whom this email
> > has been addressed. If you are not the intended recipient, or the person
> > responsible for delivering it to the intended recipient, you are hereby
> > notified that any disclosure, copying, distribution or use of any of the
> > information is strictly prohibited. If you have received this transmission
> > in error, please let us know immediately and delete the email, including
> > any attachments thereto, from your computer.
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en