【问题标题】:Save email config in CodeIgniter and changing a setting when needed在 CodeIgniter 中保存电子邮件配置并在需要时更改设置
【发布时间】:2013-07-30 15:53:36
【问题描述】:

今天我在 CodeIgniter 中尝试了电子邮件类。根据文档,我已将电子邮件 $config 保存在 config/email.php 中。然后我像往常一样使用电子邮件类。是不是像这样:

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_usersmtp_pass 字段。我该怎么做?我想避免重写一个全新的配置数组。

【问题讨论】:

    标签: php codeigniter smtp


    【解决方案1】:

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

         $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 设置的任何值,或者将它们传递给控制器​​。

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

    【讨论】:

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

    可以覆盖来自 config/email.php 的设置。您需要$this-&gt;load-&gt;library('email'); 从 config/email.php 中引入设置。然后,您可以为要覆盖的设置创建一个新的配置数组,并调用 $this-&gt;email-&gt;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');
    
        $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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      • 2016-11-02
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多