Welcome Guest, Not a member yet? Register   Sign In
Best way to send text & email notifications from within a controller
#1

What would the best way to send email and text notifications from within a controller be?

For example, a user does something and someone else is setup to receive an email/sms notification after it is completed. I'm using DOMPDF to generate PDFs with no issues and use Sendgrid & Twilio for sending out notifications. The thing is that the notifications require views to be loaded in order to create the html. 

Should I create a library with a function for each notification?

Thanks all!
Reply
#2

It looks like I've found the answer to my question.

Returning views as data
There is a third optional parameter lets you change the behavior of the function so that it returns data as a string rather than sending it to your browser. This can be useful if you want to process the data in some way. If you set the parameter to true (boolean) it will return data. The default behavior is false, which sends it to your browser. Remember to assign it to a variable if you want the data returned:
Code:
$string = $this->load->view('myfile', '', true);



I can build my html for the PDF this way.

Let me know if you think this is a bad way to do this.

Thank you!
Reply
#3

I'm doing that for sending "forgot my password" email. So it works just fine.
Reply
#4

(02-14-2020, 12:06 PM)jreklund Wrote: I'm doing that for sending "forgot my password" email. So it works just fine.

Yes, it's perfect, I had it working about a minute after I posted the reply. I've been generating some alerts via AJAX requests for a while now but am starting to implement automated reporting and more advanced notification features. 

Would it be a bad idea to use views this way inside model functions?
Reply
#5

It can be loaded there, but I generally don't want to load that type of data inside a model. In that case I would build a library instead. I think it would be cleaner to incorporate it there.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB