永久更改Codeigniter配置变量

时间:2015-04-11 23:01:08

标签: codeigniter variables config

我有配置变量的问题,我使用PHPMailer通过smtp发送电子邮件,所以我必须根据我的帐户信息更改配置变量。

$config['smtp_host']        = 'smtp.ipage.com';
$config['smtp_user']        = 'my_email@my_domain.com';
$config['smtp_pass']        = 'my_password';

我的帐户信息存储在数据库中,所以当我将这些信息放在email.php配置文件中时。该脚本工作正常,但是当我从Controller更改脚本时,脚本不起作用。

$this->config->load('email');
$this->config->set_item('smtp_user', $from); // $from $pass $host comes from db
$this->config->set_item('smtp_pass', $pass);
$this->config->set_item('smtp_host', $host);
$this->load->library('email');

当我从配置文件中删除变量并且我回显$this->config数组时,变量(从控制器)被更改,但是当我想发送它不发送的电子邮件时。它就像变量在几秒钟后丢失一样,如果变量在配置文件中,则发送电子邮件 所以,我想长期改变这些变量。

1 个答案:

答案 0 :(得分:0)

正如documentation告诉你的那样。首先删除任何config/email.php文件。在控制器的操作中,请执行以下操作:

$this->load->library('email');
$config['smtp_user'] = $from; // $from $pass $host comes from db
$config['smtp_pass'] = $pass;
$config['smtp_host'] = $host;
$config['protocol']= 'smtp';
$this->email->initialize($config);

$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com'); 
$this->email->cc('another@another-example.com'); 
$this->email->bcc('them@their-example.com'); 

$this->email->subject('Email Test');
$this->email->message('Testing the email class.');  

$this->email->send();