2013-07-30 31 views
3

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

配置/ email.php:在CodeIgniter中保存电子邮件配置并在需要时更改设置

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

采用这种设置,一切工作正常,但现在如果我想改变一些设置,我会怎么做?例如,我想发送电子邮件来自另一个帐户和部分网站:我需要能够更改smtp_usersmtp_pass字段。我将如何做到这一点?我想避免重写一个全新的配置数组。

回答

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

如果要更改配置,只是做上述,并设置每个值参数传递给任何你希望他们通过POST或者传递给控制器​​的参数。

我不确定你是否在我发布后第一次编辑你的问题,或者我错过了它,但现在我看到'我想避免重写一个全新的配置数组'。我不知道任何其他方式。

+0

嗨,我没有编辑我的问题,是的这就是我prety多想什么“我想以避免重写一个全新的配置数组'现在' – Krimson

+0

只需传递你想覆盖的值,而不是整个数组 –

1

覆盖来自config/email.php的设置是可能的。您需要$this->load->library('email');才能从config/email.php中引入设置。然后,您可以为要覆盖的设置创建一个新的配置阵列,并拨打$this->email->initialize($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('[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(); 
} 
相关问题