cakephp发送大量电子邮件cron工作

时间:2012-11-27 13:49:05

标签: email shell cakephp permissions cron

我希望使用cakephp shell cron job发送大量电子邮件。 我开始向一个人发送邮件。它不起作用。以下是我的代码:

    <?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' => 'tempEmail@gmail.com', 
            'template' => 'test' 
        )); 
    } 

/** 
 * Send just one email 
 * i call this function in shell
 */ 
    function sendMeAnEmail() { 
         //~ $this->out('Hello world.');
        return $this->Email->send(array( 
            'to' => 'tempEmail1@yahoo.in', 
            '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('abc@yahoo.in', 'xyz@yahoo.com'); 
        $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
我在哪里错了?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我知道这是很久以前的事了,但这可能对某人有所帮助。您的tmp / cache目录应该具有写入权限以及其中的文件。您还应该chown -R yourUser:www-data该目录和tmp / logs以防止此错误,即为Apache提供组所有权。

除此之外,更常见的是将您的Apache网站空间放在/ var / www /中并执行类似操作,例如将您的应用程序目录放在/ opt中,如果您希望将代码放在Apache空间之外。