CodeIgniter Forums
Best way of setting a global email recipient - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Best way of setting a global email recipient (/showthread.php?tid=11208)



Best way of setting a global email recipient - El Forum - 08-30-2008

[eluser]prezet[/eluser]
Hi

What is the preferred method for setting a global variable for the recipient of the email class?

I've tried to do it in the email config file, but that doesn't seem to work:

Code:
$config['to'] = '[email protected]';

Basically, all the emails from the site I'm building are only ever going to be going to one address, and I'd like an easy way of maintaining that address should it need to change.

Thanks.


Best way of setting a global email recipient - El Forum - 08-30-2008

[eluser]Référencement Google[/eluser]
Can you post your code so we can help?


Best way of setting a global email recipient - El Forum - 08-30-2008

[eluser]prezet[/eluser]
Sorry.

Well, I've got a few controllers that make use of the Email class, and I construct the rules like so:

Code:
// Set email rules
$this->email->from('[email protected]', 'someone');
$this->email->to('[email protected]');
$this->email->subject('Hello');
$this->email->message('How are you?');

So rather than me having to set the:

Code:
$this->email->to('[email protected]');

.. in each controller, I'd like to have it globally defined somewhere, ideally in the Email config file, so that ALL email's sent from the Email class will go to that recipient. Otherwise, if the recipients email changes, I've then got to go through each of the controllers and change it, rather than just making the change in a centralised file.

Thanks.


Best way of setting a global email recipient - El Forum - 08-30-2008

[eluser]Référencement Google[/eluser]
The user guide says this:

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() function if you save your preferences in a config file.

Have you tried it exactly like it is explained?


Best way of setting a global email recipient - El Forum - 08-30-2008

[eluser]prezet[/eluser]
Yes. And the 'to' method is not part of the preferences. It's a method.


Best way of setting a global email recipient - El Forum - 08-30-2008

[eluser]Référencement Google[/eluser]
I see then 2 possible solutions to this:

- Set a $config['mail_to'] = '[email protected]'; and fetch it with $this->config->item('mail_to') where necessary (I didn't tested if it was possible to fetch it like it when you put this config in the config/email.php file

- Set a constant in your config file (or in the constants.php file): define('MAIL_TO', '[email protected]');

Maybe someone will have a better solution to this, but that's how I would do it.


Best way of setting a global email recipient - El Forum - 08-30-2008

[eluser]prezet[/eluser]
Thanks - they were the types of possible solutions I was after. Just didn't know if there was a best practice to this sort of thing?


Best way of setting a global email recipient - El Forum - 08-30-2008

[eluser]Référencement Google[/eluser]
There is nothing wrong using them.