CodeIgniter Forums
Email Library Constructor - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Email Library Constructor (/showthread.php?tid=61931)



Email Library Constructor - 330Root - 06-01-2015

Having trouble with this, maybe someone can help.

I have a Library that handles on my email functions.  I have 3 environments setup.  Development, Test & Production.  What I am trying to do is have different application names and mailto addresses depending on the environment.  I've tried to do this through constructor but no luck.  Basically, I don't want test emails going to real users and I'd like to better distinguish what is test from what is actual data for my testers who are also users...

PHP Code:
public function __construct()
 
   {
 
       parent::__construct();
 
$this->load->library('email');

if(
ENVIRONMENT == 'development')
 {
 
$emailto 'developer@email';
 
$appname '***DEVELOPMENT APP***';
 }
 elseif(
ENVIRONMENT == 'testing')
 {
 
$emailto 'test@email';
 
                       $appname '*TEST APP*';
 }
 elseif(
ENVIRONMENT == 'production')
 {
 
$emailto 'dynamic@email';
 
                       $appname 'LIVE APP';
 }
 
   }

public function 
email()
 {
 
$message 'Email Message.';

 
$this->email->from($XXX'XXX');
 
$this->email->to($emailto);
 
$this->email->subject($appname.' -> Email Subject');
 
$this->email->message($message);
 
$this->email->send();
 } 

I can get this to work if I put the code directly in each function but it would be so much nicer if it could just all be in one spot!!

Sorry for any formatting problems, I am not a forum pro...  THANK YOU.


RE: Email Library Constructor - Avenirer - 06-02-2015

Create three directories inside config directory ("development", "testing", "production"). Inside each of them create a file named email.php, and there put the particular configuration you want to use.


RE: Email Library Constructor - JayAdra - 06-02-2015

Avenirer has the ideal solution, but if you want to keep the config within the library for some reason, you could do something like:

PHP Code:
protected $emailto;
protected 
$appname;

public function 
__construct()
 
   {
 
       parent::__construct();
 
$this->load->library('email');

if(
ENVIRONMENT == 'development')
 {
 
$this->emailto 'developer@email';
 
$this->appname '***DEVELOPMENT APP***';
 }
 
   }

public function 
email()
 {
 
$message 'Email Message.';

 
$this->email->from($XXX'XXX');
 
$this->email->to($this->emailto);
 
$this->email->subject($this->appname.' -> Email Subject');
 
$this->email->message($message);
 
$this->email->send();
 } 



RE: Email Library Constructor - 330Root - 06-02-2015

Jay that worked PERFECT! Thank you!!! If possible, what is the reason that $this-> needs to be used and regular variables don't work?


RE: Email Library Constructor - Avenirer - 06-02-2015

@330Root That is because... OOP?... You are actually working with properties in classes, not with variables. It would take a lot to explain. You should look for OOP tutorials.

In short, those properties are accessible across your class (inside the methods) only if you put $this-> before their name. By using $this->property you are actually tell a method to look for a property of the class with the name of "property".


RE: Email Library Constructor - 330Root - 06-02-2015

(06-02-2015, 07:08 AM)Avenirer Wrote: @330Root That is because... OOP?... You are actually working with properties in classes, not with variables. It would take a lot to explain. You should look for OOP tutorials.

In short, those properties are accessible across your class (inside the methods) only if you put $this-> before their name. By using $this->property you are actually tell a method to look for a property of the class with the name of "property".

That helps.  Thank you very much.  Always trying to learn.