Welcome Guest, Not a member yet? Register   Sign In
Where to put an email function
#1

[eluser]CARP[/eluser]
Hi guys
I have built an app for government that allow users to request a new e-mail address account related to government.
This CI app sends mail notifications to mail server admins when a new user requests an account.

I have this code cloned in some controllers

Code:
$this->load->library('email');            
    $this->email->from('[email protected]', 'Acc. req. system');
    $this->email->to('[email protected]');
    $this->email->cc('[email protected]');
    $this->email->bcc('[email protected]');
    $this->email->subject('[EMAIL] New request');
    $this->email->message('blabla');
    $this->email->send();
    //echo $this->email->print_debugger();

I'd like to build a function somewhere with some parameters for sending mails, for example:

Code:
send_email($to, $from, $cc, $bcc, $message);

Where should I build this function? Inside a controller? How should then I call it from another or the same controller?

Thanks a lot in advance. Hope you've understood my question.
#2

[eluser]Sarre[/eluser]
How about inside the library itself?

Then from the controller you can call it like this:

$this->email->send_email($to, $from, $cc, $bcc, $message)

[Edit] woops, off course the e-mail library is a CI-native library, i had forgotten that.
Still you can extend that library
See libraries (> extending native libraries)
#3

[eluser]CARP[/eluser]
Thanks Sarre for the tip
After reading that topic in the user_guide, got other question

Could I call & use the std email library from my own library? I think that rather than extending the email library, I'll build a "common_functions" library with some snippets commonly used...

Thanks again
#4

[eluser]sophistry[/eluser]
depending how you want to set it up you could also make a master controller (a front-controller) by subclassing all of your controllers to one master controller then put the snippet functions in there.

also, a helper is a good place for code that simply "wraps" other code. read up on helper in the guide.

if you want to still build your own separate library that talks to CI the user guide referenced above has info on that too. search on get_instance.
#5

[eluser]CARP[/eluser]
Aha... cool
I just wanted to know the best way to build a "snippet" code repository I could use and call frequently from my controllers
The "subclassing controllers" concept is too far from my knowledge. I think I'll go by building a helper or library to put my common used functions

Thanks again Smile
#6

[eluser]sophistry[/eluser]
found this code by wiredesignz that illustrates multiple inheritance in CI controllers nicely.
see
Quote:
Code:
application/libraries/MY_Controller.php
class MY_Controller extends Controller
{
    function MY_Controller()
    {
        parent::Controller();
    }
application/controllers/main.php
class Main extends MY_Controller
{
    function Main()
    {
        parent::MY_Controller();
    }
the top post on this page of this enlightening OO-design oriented thread from a few months back.
#7

[eluser]evanwalsh[/eluser]
I would put something like that in a "common" model. That's how I do things, but I'm not saying it's the best.
#8

[eluser]sophistry[/eluser]
@evanwalsh - yes, that would work too - you can put code pretty much anywhere in CI! ;-) but, devs tend to think of models in terms of a shell around a database. since the requirement is to send email that kind of breaks down the organization. it would work, but putting non-db oriented code in a model is kind of like hiding it.
#9

[eluser]CARP[/eluser]
@sophistry
you're right. I've finally built a helper, and it is working great

Thanks 2 everyone for helping
#10

[eluser]Gabriel Jones[/eluser]
@CARP - Sounds to me you would like to override the send_email helper function. In which case you may want to create a file called MY_email_helper.php and create the override there. Where 'MY_' is your subclass prefix you define in your config. I have a similar situation, and after reviewing php.net I found it to be fairly straight forward.




Theme © iAndrew 2016 - Forum software by © MyBB