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

发布于 2024-12-02 00:46:56 字数 1727 浏览 0 评论 0原文

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

这是一个示例

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 = '[email protected]';
        $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() /var/www/app/controllers/components/remote.php 第 19 行

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

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

那么对于这种情况有什么理想的想法吗?

I have a component that connects to a remove data base for several different reasons. However the remote data base is not guaranteed up, so in case it is down I want to send an email alerting someone that it failed.

Here is an example

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 = '[email protected]';
        $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;
    }
}

The problem is the component class deosn't have the set() method like the controller class does. So I get this error:

Fatal error: Call to undefined method RemoteComponent::set() in
/var/www/app/controllers/components/remote.php on line 19

I need to set the data for the view the email is going to use (not the view rendered to the user)

I want to handle this internally in the component because many controllers may use this component for different reasons and this component handles all the connections to the remote db.

So any ideas on what would be ideal for this situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一影成城 2024-12-09 00:46:56

我不确定这是否是最好的方法,我通常在 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,....);
    }

希望这有帮助

I'm not sure if this is the best way to proceed by I usually create a method in the 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);

    }

I put it in the AppController because I use it in different controllers. So in your case, id save the reference of the Controller (or pass it as an argument), something like this

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

or

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

Hope this helps

夏天碎花小短裙 2024-12-09 00:46:56

我通过捕获对组件的引用并调用其中的 set 来使其工作:

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

...

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

I got it to work by capturing a reference to the component and calling set in it instead:

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

...

$this->Controller->set(compact('conn_fail_data'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文