CakePHP设置用于从组件发送电子邮件的数据

时间:2011-08-30 15:16:44

标签: email cakephp components cakephp-1.3

我有一个连接到删除数据库的组件,原因有很多。但是,远程数据库无法保证,因此如果它已关闭,我想发送一封电子邮件,提醒某人它失败了。

这是一个例子

App::import('Vendor', 'remote_connection_pdo');

class RemoteComponent extends Object 
{
    public $components = array('Email');

    private function remote_connection_failed($data)
    {
        //set info about what was processing when connection failed
        $this->set(compact('data'));

        $this->Email->to = 'team@example.com';
        $this->Email->subject = 'Remote Connection Failed';
        $this->Email->template = 'remote_connect_failure';
        $this->Email->sendAs = 'html';

        $this->Email->send();
    }

    public function doSomething($data)
    {
        try
        {
            $pdo = new RemoteConnectionPDO(RemoteConnectionPDO::getConnection());
        }
        catch(PDOException $e)
        {
            $conn_fail_data = array(
                'While Processing'  => 'Doing something',
                'Other Info'        => $data['Other']['info'],
                'foo'               => 'bar',
            );
            $this->remote_connection_failed($conn_fail_data);

            return false;
        }

        //do something
        //...
        return true;
    }
}

问题是组件类没有像控制器类那样的set()方法。所以我收到了这个错误:

  

致命错误:调用未定义的方法RemoteComponent :: set()in   第19行/var/www/app/controllers/components/remote.php

我需要设置电子邮件将要使用的视图的数据(而不是呈现给用户的视图)

我想在组件内部处理这个问题,因为许多控制器可能出于不同的原因使用此组件,并且该组件处理与远程数据库的所有连接。

那么关于什么是理想的情况的任何想法?

2 个答案:

答案 0 :(得分:2)

我不确定这是否是最好的方法,我通常在AppController中创建一个方法:

protected function __sendMail($from,$to,$bcc,$subject,$replyto,$template,$attachments = array(),$headers = array()){
        // SMTP Options
        $this->Email->smtpOptions = array(
            'port'=>MAIL_PORT, 
            'timeout'=>'30',
            'host' => MAIL_HOST,
            'username'=>SENDER_MAIL,
            'password'=>SENDER_PASS
        );

        // Set delivery method
        $this->Email->delivery = 'smtp';
        $this->Email->SMTPAuth = true;
        $this->Email->SMTPSecure = 'tls';
        $this->Email->charset  = 'UTF-8';
        $this->Email->to = $to;
        $this->Email->bcc = $bcc;
        $this->Email->subject = $subject;
        $this->Email->replyTo = $replyto;
        $this->Email->from = $from;
        $this->Email->template = $template;
        $this->Email->header($headers); 

        //Send as 'html', 'text' or 'both' (default is 'text')
        $this->Email->sendAs = 'both';

        $this->Email->attachments = $attachments;

        // Do not pass any args to send()
        $this->Email->send();

        // Check for SMTP errors.
        $this->set('smtp_errors', $this->Email->smtpError);

    }

我把它放在AppController中,因为我在不同的控制器中使用它。所以在你的情况下,id保存Controller的引用(或将其作为参数传递),类似这样的

class RemoteComponent extends Object 
{
    function initialize(&$controller) {
        $this->controller = $controller;
    }

    private function remote_connection_failed($data){
            $this->controller->set('data',$data); //your data
            $this->controller->__sendMail($from,$to,....);
    }

class RemoteComponent extends Object 
{
    private function remote_connection_failed($data,$controller){
            $controller->set('data',$data); //your data
            $controller->__sendMail($from,$to,....);
    }

希望这有帮助

答案 1 :(得分:1)

我通过捕获对组件的引用并在其中调用set来实现它:

public function initialize(&$Controller) 
{
    $this->Controller = $Controller;
}

...

$this->Controller->set(compact('conn_fail_data'));