CodeIgniter Forums

Full Version: Email Library Constructor
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 '[email protected]';
 
$appname '***DEVELOPMENT APP***';
 }
 elseif(
ENVIRONMENT == 'testing')
 {
 
$emailto '[email protected]';
 
                       $appname '*TEST APP*';
 }
 elseif(
ENVIRONMENT == 'production')
 {
 
$emailto '[email protected]';
 
                       $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.
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.
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 '[email protected]';
 
$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();
 } 
Jay that worked PERFECT! Thank you!!! If possible, what is the reason that $this-> needs to be used and regular variables don't work?
@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".
(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.