# \[Solved\]Check content of a view and send mail if not ok

**URL:** <https://discourse.cakephp.org/t/solved-check-content-of-a-view-and-send-mail-if-not-ok/5269>\
**Category:** Need Help\
**Created:** [November 15, 2018, 12:09pm UTC](https://discourse.cakephp.org/t/solved-check-content-of-a-view-and-send-mail-if-not-ok/5269 "2018-11-15T12:09:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![m.depoli](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@m.depoli](https://discourse.cakephp.org/u/m.depoli)\
**Post date:** [November 15, 2018, 12:09pm UTC](https://discourse.cakephp.org/t/solved-check-content-of-a-view-and-send-mail-if-not-ok/5269/1 "2018-11-15T12:09:49Z")

</div>

Hi everyone,  
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

---

<div class="post-metadata">

**Author:** ![m.depoli](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@m.depoli](https://discourse.cakephp.org/u/m.depoli)\
**Post date:** [November 20, 2018, 9:04am UTC](https://discourse.cakephp.org/t/solved-check-content-of-a-view-and-send-mail-if-not-ok/5269/2 "2018-11-20T09:04:39Z")

</div>

No one who has any ideas on how to do it?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [November 20, 2018, 9:57pm UTC](https://discourse.cakephp.org/t/solved-check-content-of-a-view-and-send-mail-if-not-ok/5269/3 "2018-11-20T21:57:03Z")

</div>

Well, components can be accessed from controllers, not views, so the approach you’re describing violates the MVC concept. This very much sounds like the sort of thing that middleware would handle, but you’d need to upgrade to at least CakePHP 3.3 to get middleware support, I believe. So, maybe no surprise that you haven’t got any suggestions so far.

When you say “must no do anything else”, what does “anything” include? Should it stop the database from being updated if it was an edit that just happened? Because doing anything about that at the time of the view is far too late.

If you just mean that no output should be sent to the user, one possibility would be to trigger an event, with a handler registered via your AppController. The handler could send the email and then throw an exception. Bit of a hackish way, but it should work, I think…

---

<div class="post-metadata">

**Author:** ![m.depoli](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@m.depoli](https://discourse.cakephp.org/u/m.depoli)\
**Post date:** [November 21, 2018, 11:20am UTC](https://discourse.cakephp.org/t/solved-check-content-of-a-view-and-send-mail-if-not-ok/5269/4 "2018-11-21T11:20:16Z")

</div>

Thank you for your reply,  
I solved the following way:

at the top of myHelper I put:

> App::uses(‘CakeEmail’, ‘Network/Email’);

then I rewritten debug\_content like this:

> public function debug\_content($s){  
> $pos=strpos($s, “\<div class="box-body"\>”);  
> if ($pos === false) {  
> echo “Layout error!   
> An e-mail been sent to our development team, soon the problem will be solved , thanks!”;  
> $Email = new CakeEmail();  
> $Email-\>config(  
> array(  
> ‘port’=\>‘xx’,  
> ‘timeout’=\>‘xx’,  
> ‘host’ =\> ‘host.ext’,  
> ‘username’=\>‘username’,  
> ‘password’=\>‘password’,  
> ‘emailFormat’ =\> ‘html’,  
> ‘transport’ =\> ‘Smtp’  
> )  
> );
> 
> ```
> $Email->from('no-replay@myhost.ext');
> $Email->to('mymail@myhost.ext');
> $Email->subject('CAKEPHP layout DEBUG');
> $Email->send("error in " . $_SERVER["REQUEST_URI"] . " plan solution as soon as possible");
> 
> ```
> 
> }  
> }

and this works …  
I do not know if it violates MVC but it works, otherwise I do not know how to solve it with 2.9, I did not need anything else … write on the db … maybe to make a sort of log … I had not really thought about it … interesting idea … but I will start to think about it if it should serve me … and certainly not with 2.9

Thank you!  
Max
