Welcome Guest, Not a member yet? Register   Sign In
Email Library Constructor
#1

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.
Reply
#2

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.
Reply
#3

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();
 } 
Reply
#4

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

(This post was last modified: 06-02-2015, 07:12 AM by Avenirer.)

@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".
Reply
#6

(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. 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB