CodeIgniter Forums

Full Version: Use 2 email configuration
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need to change email accounts, send them, and then return to the default settings.

I know that it is possible to modify configurations on the fly by passing an array of values to $this->email->initialize($ config);

Is there a way to have two or more different configurations, or do you have a way to return to the default configurations without having to initialize everything with the default parameters?

Thanks
The main email config file - APPATH/config/email.php - can only contain one set of options. but you can create other config files and load them explicitly passing those values to email->initialize().

But you will have to retrieve APPATH/config/email.php values and send them to $this->email->initialize($ config); to return to the default setup. No way around that I can see.
(10-31-2017, 08:47 AM)dave friend Wrote: [ -> ]The main email config file - APPATH/config/email.php - can only contain one set of options. but you can create other config files and load them explicitly passing those values to email->initialize().

But you will have to retrieve APPATH/config/email.php values and send them to  $this->email->initialize($config); to return to the default setup. No way around that I can see.
I think I'm wrong, I created a file for configuring the second account, load it with $this->config->load(), then call $this->email->initialize($config), but I do not get what I expect, I'm definitely mistaken, can you help me?

Thanks
I got interested on how I would do this and here is something I've just tested.

1. Create within the config/ folder two configuration files, email.php and email_2.php and fill them with the corresponding data for the two email accounts.

2. Within libraries/ directory create a file Email_2.php with the following content:
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

get_instance()->load->library('email');

// This name could be 'CI_Email' or 'MY_Email', or something else as a result.
$_email_class_name = get_class(get_instance()->email);

class_alias($_email_class_name, '_Email_2');

class Email_2 extends _Email_2 {

}

3. Within the controller Welcome.php or other controller test with this fragment of code:
Code:
$this->load->library('email');
        var_dump(get_object_vars($this->email));

        echo '<br />';

        $this->load->library('email_2');
        var_dump(get_object_vars($this->email_2));

You would see that both library instances have different properties, according to their configuration files.
I sincerely wanted to avoid too many turns and requests for instances, I did a try like this:

I've created in addition to the default file email.php in the config folder, a second file with the other configuration.

so to use the second account I do so:

Code:
$config_pec = $this->config->load('email_pec', true);
$config_pec = $this->config;
// Send new config to email class
$this->email->initialize( $config_pec->config['email_pec'] );

Send the amil I need, and then to return to the initial configuration, I do the same thing by referring to the default email.php file

Can it be a solution?

Do you see things that could also cause problems in the future?