2012-11-27 110 views
0

我想用cakephp shell cron作業發送羣發郵件。 我開始將郵件發送給一個人。它不工作。下面是我的代碼:cakephp發送羣發郵件cron作業

<?php 
error_reporting(0); 
class MyShell extends Shell { 
/** 
* List of tasks for this shell 
* 
* @var array 
*/ 
    var $tasks = array('Email'); 

/** 
* Email task 
* 
* @var EmailTask 
*/ 
    var $Email; 

/** 
* Startup method for the shell 
* 
* Lets set some default params for the EmailTask 
* 
*/ 
    function startup() { 
     $this->Email->settings(array( 
      'from' => '[email protected]', 
      'template' => 'test' 
     )); 
    } 

/** 
* Send just one email 
* i call this function in shell 
*/ 
    function sendMeAnEmail() { 
     //~ $this->out('Hello world.'); 
     return $this->Email->send(array( 
      'to' => '[email protected]', 
      'subject' => 'Talking to myself' 
     )); 
    } 

/** 
* Send multiple emails, change a few variables on the fly 
* and test that we can 'set' variables to the view 
* 
//~ */ 
    function sendMyFriendsAnEmail() { 
     $myFriends = array('[email protected]', '[email protected]'); 
     $this->Email->settings(array('subject' => 'Hello friends')); 
     foreach ($myFriends AS $friend) { 
      $this->Email->set('someVar', $friend); 
      $this->Email->send(array( 
       'to' => $friend, 
       'subject' => 'Hello ' . $friend 
      )); 
     } 
    } 
} 
?> 

email.php

<?php 
App::import('Core', 'Controller'); 
App::import('Component', 'Email'); 

class EmailTask extends Shell { 
/** 
* Controller class 
* 
* @var Controller 
*/ 
    var $Controller; 

/** 
* EmailComponent 
* 
* @var EmailComponent 
*/ 
    var $Email; 

/** 
* List of default variables for EmailComponent 
* 
* @var array 
*/ 
    var $defaults = array( 
     'to'  => null, 
     'subject' => null, 
     'charset' => 'UTF-8', 
     'from'  => null, 
     'sendAs' => 'html', 
     'template' => null, 
     'debug'  => false, 
     'additionalParams' => '', 
     'layout' => 'default' 
    ); 

/** 
* Startup for the EmailTask 
* 
*/ 
    function initialize() { 
     $this->Controller =& new Controller(); 
     $this->Email =& new EmailComponent(null); 
     $this->Email->startup($this->Controller); 
    } 

/** 
* Send an email useing the EmailComponent 
* 
* @param array $settings 
* @return boolean 
*/ 
    function send($settings = array()) { 
     $this->settings($settings); 
     return $this->Email->send(); 
    } 

/** 
* Used to set view vars to the Controller so 
* that they will be available when the view render 
* the template 
* 
* @param string $name 
* @param mixed $data 
*/ 
    function set($name, $data) { 
     $this->Controller->set($name, $data); 
    } 

/** 
* Change default variables 
* Fancy if you want to send many emails and only want 
* to change 'from' or few keys 
* 
* @param array $settings 
*/ 
    function settings($settings = array()) { 
     $this->Email->_set($this->defaults = array_filter(am($this->defaults, $settings))); 
    } 
} 
?> 

輸出:

PHP Warning: 

SplFileInfo::openFile(/mnt/public_html/directory/web/cakephp/app/tmp/cache/persistent/directory_cake_core_file_map): failed to open stream: Permission denied in /mnt/public_html/directory/web/cakephp/lib/Cake/Cache/Engine/FileEngine.php on line 313 

Warning: SplFileInfo::openFile(/mnt/public_html/directory/web/cakephp/app/tmp/cache/persistent/directory_cake_core_file_map): failed to open stream: Permission denied in /mnt/public_html/directory/web/cakephp/lib/Cake/Cache/Engine/FileEngine.php on line 313 

這裏我收到錯了嗎?我如何解決這個問題?

+0

您是否檢查過您打開的文件的權限? – ChrisBint

+0

是的,他們都擁有讀寫權限 – z22

回答

0

我知道這是很久以前的事情,但這可能有助於某人。您的tmp/cache目錄應該具有寫入權限以及其中的文件。您還應該chown -R yourUser:www-data該目錄和tmp/logs以防止出現此錯誤,即將組所有權交給Apache。另外,更常用的方法是將你的Apache webspace放在/ var/www /中,並且執行類似於將你的app目錄放在/ opt中的方式,如果你想讓你的代碼在Apache空間之外。