In CAKEPHP Check content of all view and send mail if not ok

51 views Asked by At

With cakephp 2.9 I have this need:

I should check if there is a specific tag on the page that appears.

if it is present, must not do anything else must send me an email.

At this point I think to create a helper and recall a method in the default.ctp

with something like:

$s = this-> fetch (‘content’);
$result_check = $this->myHelper->debug_content ($s); 
echo $s;

In the myHelper the function:

public function debug_content( $s) { 
  $pos = strpos ( $s, "<div class = \"box-body\">"); 
  if ( $pos === false) { echo “Error tag is not present!”; 
  return false; 
}

In the AppController:

public $helpers = array (…, ‘myHelper’);

and up to here ok … but now?

How do I recall the component Email (personalized by me) to send an email?

And where do I call it?

How would you do?

Thank you,

Max

2

There are 2 answers

0
q-jack On BEST ANSWER

I dont know if I got your question right. But you can implement the needed e-mail sending function into your function where you are checking for the tag.

 App::uses('CakeEmail', 'Network/Email');

public function debug_content( $s) { 
  $pos = strpos ( $s, "<div class = \"box-body\">"); 
  if ( $pos === false) { echo “Error tag is not present!”; 
 $Email = new CakeEmail();
 $Email->emailFormat('html');
 $Email->template('default');
 $Email->from('[email protected]');
 $Email->to('[email protected]');
 $Email->subject('YOUR SUBJECT');
 $Email->send();
  return false; 
}
0
Timur Asaliev On

Implement a listener for the View.afterRenderFile event and see if this solves the issue. Have a look at the source code of the View class, you can see that this callback receives the view file name and the evaluated contents during the call to View::render(). You can then use this listener to check the contents for your tag and send that email if necessary.