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

Php 在CodeIgniter中保存电子邮件配置,并在需要时更改设置,php,codeigniter,smtp,Php,Codeigniter,Smtp,今天我在CodeIgniter中试用了电子邮件类。根据文档,我已将电子邮件$config保存在config/email.php中。然后我就像平常一样使用email类。那么是这样的: config/email.php: <?php $config = Array( 'protocol' => 'smtp', 'smtp_host' => 'ssl://smtp.gmail.com', 'smtp_port' => 4

今天我在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();
}
<?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');

    $config = Array(
        'smtp_user' => '******',
        'smtp_pass' => '******'
    );

    $this->email->initialize($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();
}

使用此设置,一切正常,但现在如果我想更改一些设置,我将如何更改这些设置?例如,我想从网站中的其他帐户发送电子邮件:我需要能够更改
smtp_用户
smtp_通行证
字段。我该怎么做?我希望避免重写一个全新的配置数组。

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

     $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将每个参数的值设置为您想要的值,或者设置为以何种方式传递给控制器


我不确定你是在我发布后第一次编辑了你的问题,还是我错过了,但我现在看到了“我想避免重写一个全新的配置数组”。我不知道还有其他方法。

可以覆盖config/email.php中的设置。您需要
$this->load->library('email')
从config/email.php引入设置。然后,您可以为要覆盖的设置创建一个新的配置数组,并调用
$this->email->initialize($config)
以应用这些设置

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

    $config = Array(
        'smtp_user' => '******',
        'smtp_pass' => '******'
    );

    $this->email->initialize($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();
}

嗨,我没有编辑我的问题,是的,这正是我想要的“我想避免“现在”重写一个全新的配置数组”,只传递你想要覆盖的值,而不是整个数组