php-将电子邮件配置保存在CodeIgniter中,并在需要时更改设置

今天,我在CodeIgniter中试用了电子邮件类.根据文档,我已将电子邮件$config保存在config / email.php中.然后,我只照常使用email类.所以是这样的:

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'
    );
?> 

一些控制器:

public function sendMessage(){
    $this->load->library('email');
    $this->email->set_newline("\r\n");
    $this->email->from('me@here.comk', 'My Name');
    $this->email->to("someone@somewhere.com");
    $this->email->subject('A test email from CodeIgniter using Gmail');
    $this->email->message("A test email from CodeIgniter using Gmail");
    $this->email->send();
}

使用此设置,一切正常,但是现在,如果我想更改某些设置,该怎么做?例如,我想从网站中和网站的其他帐户发送电子邮件:我需要能够更改smtp_user和smtp_pass字段.我该怎么做?我想避免重写整个新的配置数组.

解决方法:

在数组中创建配置并在控制器中加载电子邮件库时添加数组:

     $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('me@here.comk', 'My Name');
    $this->email->to("someone@somewhere.com");
    $this->email->subject('A test email from CodeIgniter using Gmail');
    $this->email->message("A test email from CodeIgniter using Gmail");
    $this->email->send();

如果要更改配置,只需执行上述操作,然后将每个参数的值设置为您希望通过POST传递的值,或者将其传递给控制器​​即可.

我不确定您是在发布后还是第一次错过问题时还是第一次错过了问题,但是现在我看到“我想避免重写整个新的配置数组”.我没有其他办法.

上一篇:PHP-Codeigniter:验证两个日期字段


下一篇:php-如果表单字段输入为空,则不更新数据库CodeIgniter