【问题标题】:CakePHP set data for sending an email from a componentCakePHP 设置用于从组件发送电子邮件的数据
【发布时间】:2011-11-06 21:56:36
【问题描述】:

出于几个不同的原因,我有一个连接到删除数据库的组件。然而,远程数据库不能保证正常运行,所以如果它出现故障,我想发送一封电子邮件提醒某人它失败了。

这是一个例子

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 /var/www/app/controllers/components/remote.php 第 19 行

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

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

那么有什么想法最适合这种情况吗?

【问题讨论】:

    标签: email cakephp components cakephp-1.3


    【解决方案1】:

    我不确定这是否是最好的方法,我通常在 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 保存控制器的引用(或将其作为参数传递),就像这样

    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,....);
        }
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:

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

      public function initialize(&$Controller) 
      {
          $this->Controller = $Controller;
      }
      
      ...
      
      $this->Controller->set(compact('conn_fail_data'));
      

      【讨论】:

        猜你喜欢
        • 2014-01-17
        • 2016-03-10
        • 2011-11-12
        • 1970-01-01
        • 2014-02-12
        • 1970-01-01
        • 1970-01-01
        • 2013-04-08
        • 2013-04-01
        相关资源
        最近更新 更多