CodeIgniter Forums

Full Version: [Solved] Email Config File Question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have created a email.php config file in

application > config > email.php

I would like to know if I load $this->load->library('email');

Does it detect the config email.php automatic?


PHP Code:
public function send() {


$this->load->library('form_validation');

$this->form_validation->set_rules('subject''E-Mail Subject''required');
$this->form_validation->set_rules('message''E-Mail Message''required');

if (
$this->form_validation->run() == FALSE) {

$this->index();

} else {

$this->load->library('email');

switch (
$this->input->post('to')) {
                
case 
'newsletter':    

$results $this->get_news_letter_emails();
                     
   
foreach ($results as $result) {

$this->email->from(config_item('email'), config_item('website_name'));
$this->email->to($result['email']);
$this->email->subject($this->input->post('subject')); 
$this->email->message($this->input->post('message'));
                     
   
$this
->email->send();
}

$this->session->set_flashdata('success''Email Sent Success');
redirect('admin/mail');

break;

}



application > config > email.php

PHP Code:
<?php

$config
['protocol'] = 'smtp';
$config['smtp_host'] = '***';
$config['smtp_port'] = 465;
$config['smtp_user'] = '***';
$config['smtp_pass'] = '***'
Yes it does, The documentation is pretty clear about this....

http://www.codeigniter.com/user_guide/li...onfig-file

Quote:Setting Email Preferences in a Config File

If you prefer not to set preferences using the above method, you can instead put them into a config file. Simply create a new file called the email.php, add the $config array in that file. Then save the file at config/email.php and it will be used automatically. You will NOT need to use the $this->email->initialize() method if you save your preferences in a config file.
(07-24-2015, 09:01 PM)Diederik Wrote: [ -> ]Yes it does, The documentation is pretty clear about this....

http://www.codeigniter.com/user_guide/li...onfig-file


Quote:Setting Email Preferences in a Config File

If you prefer not to set preferences using the above method, you can instead put them into a config file. Simply create a new file called the email.php, add the $config array in that file. Then save the file at config/email.php and it will be used automatically. You will NOT need to use the $this->email->initialize() method if you save your preferences in a config file.


You don't load the file with:

PHP Code:
$this->load->library('email'

...because it's not a library it's a config file.
(07-25-2015, 02:50 AM)Dracula Wrote: [ -> ]
(07-24-2015, 09:01 PM)Diederik Wrote: [ -> ]You don't load the file with:
PHP Code:
$this->load->library('email'

...because it's not a library it's a config file.

The question was: does CI automatically detect the email.php config file when loading the email library. I think Diederik already gave the correct answer to that.
(07-25-2015, 02:50 AM)Dracula Wrote: [ -> ]You don't load the file with:


PHP Code:
$this->load->library('email'

...because it's not a library it's a config file.

As he was trying to point out, when you load a library, CI's loader automatically attempts to locate a config file with the same name (in all lowercase or ucfirst(strtolower($class))), including checking for environment-specific config files (if both exist, it will load the global first, followed by the environment-specific file). Then it instantiates the library and passes $config to the constructor. So, as long as the constructor handles the $config data passed from the config file(s), the library will automatically be configured according to the config file(s).

This also works with your own libraries if you define your constructor as receiving an array parameter and add a config file named after your library. For example, I recently modified Bonfire's installer library to use a config file, so the constructor looks something like this:


PHP Code:
   public function __construct($config = array())
 
   {
 
       if (! empty($config['writable_folders'])) {
 
           $this->writable_folders $config['writable_folders'];
 
       }
 
       if (! empty($config['writable_files'])) {
 
           $this->writable_files $config['writable_files'];
 
       }

 
       $this->ci =& get_instance();
 
       $this->curl_update $this->cURL_enabled();
 
       $this->php_version phpversion();
 
   

When we load the installer library, we don't have to load the config file ourselves and pass it to the library, we just tell CI to load the library. I assume that the values could be empty, because someone using Bonfire could either not add the config file or remove the entries from the file, but in a default installation the variables will be set without having to do anything else.