Welcome Guest, Not a member yet? Register   Sign In
CI4 How to create and choose between two types of email configurations
#1

How can I send an email with the default configuration (Config\Email) and immediately afterwards send a second one with another configuration (Config\Email2) and finally restore the default one?
Reply
#2

You need to create two seperate configs for the email.

CodeIgniter 4 User Guide - Setting Email Preferences

PHP Code:
$email->initialize($config1);

// Then

$email->initialize($config2); 


That should get you going.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(02-17-2022, 01:38 AM)InsiteFX Wrote: You need to create two seperate configs for the email.

CodeIgniter 4 User Guide - Setting Email Preferences

PHP Code:
$email->initialize($config1);

// Then

$email->initialize($config2); 


That should get you going.

I have already created the two configuration files for it, so should I do this?


PHP Code:
$config1 config('Email1');
$config2 config('Email2');

$email service('email');

$email->initialize($config1);

// Then send one

$eamil->initialize($config2);

// Then send two 

Doing so, however, gives me this error

Cannot use object of type Config\Email1 as array
Reply
#4

Email::initialize() accepts object instance of Config\Email or array as argument. From the error it seems you are passing neither an array or Config\Email instance.
Two possible solutions:

1) Config\Email1 and Config\Email2 should extend Config\Email. So, retain the original Config\Email provided by the framework. Then, in each Email1 and Email2,

class Email1 extends Email {} // instead of extends BaseConfig
class Email2 extends Email {} // instead of extends BaseConfig

2) Use `get_object_vars`

$email->initialize(get_object_vars($config1));
$email->initialize(get_object_vars($config2));
Reply
#5

(02-17-2022, 08:51 PM)paulbalandan Wrote: Email::initialize() accepts object instance of Config\Email or array as argument. From the error it seems you are passing neither an array or Config\Email instance.
Two possible solutions:

1) Config\Email1 and Config\Email2 should extend Config\Email. So, retain the original Config\Email provided by the framework. Then, in each Email1 and Email2,

class Email1 extends Email {} // instead of extends BaseConfig
class Email2 extends Email {} // instead of extends BaseConfig

2) Use `get_object_vars`

$email->initialize(get_object_vars($config1));
$email->initialize(get_object_vars($config2));

Perfect, thanks Wink
Reply




Theme © iAndrew 2016 - Forum software by © MyBB