我正在尝试像从控制器一样从 CakePHP shell 发送电子邮件.
I'm trying to send an email from a CakePHP shell just as you would from the Controller.
以下大部分代码改编自 这篇关于面包店的过时文章 及其评论.电子邮件正在发送,但是行 $controller->set('result', $results[$i]); 抛出以下通知:
Most of the code below was adapted from this dated article on the Bakery and it's comments. The email is sending, however the line $controller->set('result', $results[$i]); throws the following notices:
注意:未定义的属性:查看::$webroot/home/jmccreary/www/intranet.sazerac.com/cakephp/cake/libs/view/view.php在线 813
Notice: Undefined property: View::$webroot in /home/jmccreary/www/intranet.sazerac.com/cakephp/cake/libs/view/view.php on line 813
PHP 注意:未定义变量:导致/home/jmccreary/www/intranet.sazerac.com/cakephp/app/views/elements/email/text/nea/task_reminder_it.ctp第 2 行
PHP Notice: Undefined variable: result in /home/jmccreary/www/intranet.sazerac.com/cakephp/app/views/elements/email/text/nea/task_reminder_it.ctp on line 2
所以我没有将任何变量传递给我的电子邮件视图.
So I'm not getting any of the variables passed to my email view.
我怎样才能做到这一点,最好遵循 Cake 约定?
How can I do this, ideally following the Cake conventions?
class NotificationShell extends Shell {
var $uses = array('Employee', 'Task');
function main() {
// run if no action is passed
}
function nea_task_reminder() {
// build Task to Employee relationship
$this->Task->bindModel(array('belongsTo' => array('Employee' => array('className' => 'Employee', 'foreignKey' => 'object_id'))));
$results = $this->Task->find('all', array('conditions' => array('application_id' => 1, 'completed_by_id' => 0), 'contain' => array('Employee' => array('Contact', 'Position'))));
$count = count($results);
if ($count) {
App::import('Core', 'Controller');
App::import('Component', 'Email');
$controller =& new Controller();
$email =& new EmailComponent();
$email->startup($controller);
// send email
$email->from = Configure::read('Email.from');
$email->to = 'jmccreary@whatever.com';
$email->replyTo = 'no-reply@whatever.com';
$email->template = 'nea/task_reminder_it';
$email->sendAs = 'text';
for ($i = 0; $i < $count; ++$i) {
$email->subject = 'NEA Notification: Task Reminder for ' . $results[$i]['Employee']['Contact']['full_name'];
$controller->set('result', $results[$i]);
$email->send();
}
}
}
}
问题在于您初始化 EmailComponent 类的方式.如果您查看源代码,startup() 方法实际上没有主体,因此它什么也不做.您的控制器实际上并未分配给 EmailComponent.问题不在于 $controller->set('results', ...);.您需要使用 EmailComponent::initialize() 而不是 EmailComponent::startup().
The problem is the way you're initializing the EmailComponent class. If you look at the source code, the startup() method doesn't actually have a body so it does nothing. Your controller isn't actually assigned to the EmailComponent. The problem isn't $controller->set('results', ...);. You need to use EmailComponent::initialize() instead of EmailComponent::startup().
$controller =& new Controller();
$email =& new EmailComponent(null);
$email->initialize($controller);
来源:
这篇关于CakePHP 使用来自 Shell cronjob 的电子邮件组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
不能使用 'Object 作为类名,因为它是保留的Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作为类名,因为它是保留的 Cake 2.2.x)
OAuth 重定向后会话丢失Session is lost after an OAuth redirect(OAuth 重定向后会话丢失)
Cakephp 3.x 中的分页排序Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分页排序)
CakePHP 多个应用程序的共享核心CakePHP Shared core for multiple apps(CakePHP 多个应用程序的共享核心)
在 CakePHP 3 上登录 [ Auth->identify() ] 始终为 falLogin [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登录 [ Auth-identify() ] 始终为 false)
致命错误:允许的内存大小为 134217728 字节已用尽Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命错误:允许的内存大小为 134217728 字节已用尽