Today I experimented with the email class in CodeIgniter. I have saved my email $config in config/email.php as per the documentation. And then I just use the email class as normal. So is it something like this:
config/email.php:
<?php
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => '******',
'smtp_pass' => '******',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
?>
Some Controller:
public function sendMessage(){
$this->load->library('email');
$this->email->set_newline("\r\n");
$this->email->from('[email protected]', 'My Name');
$this->email->to("[email protected]");
$this->email->subject('A test email from CodeIgniter using Gmail');
$this->email->message("A test email from CodeIgniter using Gmail");
$this->email->send();
}
With this setup, everything works fine, but now if I wanted to change some settings how would I do that? For example, I want to send the email from a different account in and part of the website: I need to be able to change the smtp_user and the smtp_pass fields. How would I do this? I want to avoid rewriting a whole new config array.
Create the config in an array and add the array when loading the email library in your controller:
$email_config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => '******',
'smtp_pass' => '******',
'mailtype' => 'html',
'charset' => 'iso-8859-1'
);
$this->load->library('email', $email_config);
$this->email->set_newline("\r\n");
$this->email->from('[email protected]', 'My Name');
$this->email->to("[email protected]");
$this->email->subject('A test email from CodeIgniter using Gmail');
$this->email->message("A test email from CodeIgniter using Gmail");
$this->email->send();
If you want to change the config, just do the above and set the values of each parameter to whatever you want them to be via POST or however they're passed to the controller.
I'm not sure if you edited your question the first time around after I posted or if I just missed it, but I now see the 'I want to avoid rewriting a whole new config array' now. I'm not aware of any other way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With