Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$Email->initialize($config) not work! #1042

Closed
35598253 opened this issue May 23, 2018 · 5 comments
Closed

$Email->initialize($config) not work! #1042

35598253 opened this issue May 23, 2018 · 5 comments

Comments

@35598253
Copy link

35598253 commented May 23, 2018

https://bcit-ci.github.io/CodeIgniter4/libraries/email.html

Setting Email Preferences

$email = \Config\Services::email();
$config['protocol'] = 'sendmail';
$config['mailPath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordWrap'] = true;

$email->initialize($config);

OR
$email = new \CodeIgniter\Email\Email($config);

print_r($email);
the email config not change

@35598253 35598253 reopened this May 23, 2018
@gabriel-pnhr
Copy link
Contributor

gabriel-pnhr commented May 23, 2018

initialise() does not support arrays although it says in the documentation
@lonnieezell it should or just an error in the documentation?

@35598253 you can use initialse() with an instance of \Config\Email :

$config = new \Config\Email;
$config->protocol = 'sendmail';
$config->mailPath = '/usr/sbin/sendmail';

$email = new \CodeIgniter\Email\Email($config); 
// or $email->initialize($config); if it is already loaded

@samsonasik
Copy link
Member

since parameter docblock uses array, I think we can add cast to object or create a \Config\Email instance from array I think, e.g.:

use Config\Email as ConfigEmail;
// ...
	/**
	 * Initialize preferences
	 *
	 * @param ConfigEmail|array $config
	 *
	 * @return Email
	 */
	public function initialize($config)
	{
		$this->clear();
                
               if (is_array($config)) { 
                    // uses cast 
                   $config = (object) $config;
                    
                    // or create a \Config\Email instance with its properties 
                    $configEmail = new ConfigEmail();
                    foreach ($config as $key => $value) {
                         $configEmail->$key = $value;
                    }
                    $config = $configEmail;
                }
                
                // ...
       }

@lonnieezell
Copy link
Member

You guys are correct. It should support either an array or a Config\Email instance. I think @samsonasik solution is fine. Anyone want to submit a PR?

@samsonasik
Copy link
Member

@lonnieezell I've created PR #1046 for it.

@lonnieezell
Copy link
Member

Dang - you beat me to it. I just did another fix for it that is a little more elegant. Will be up shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants