CakePHP set data for sending an email from a component
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 in开发者_Go百科fo 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;
}
}
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?
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
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'));
精彩评论