使用CI电子邮件类向mail()添加其他参数

时间:2012-09-27 19:50:37

标签: php codeigniter

我正在尝试使用Codeigniters电子邮件类(使用mail()send方法)找到一种向电子邮件添加其他参数的方法。我找不到任何文档或其他信息。你们大家都知道框架内是否有办法?

PHP native mail():

mail ( string $to , string $subject , string $message [, string $additional_headers 
       [, string $additional_parameters ]] );

笨:

$this->load->library('email');
$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->additional_parameters('something_goes_here'); // this is what I need
$this->email->send();

2 个答案:

答案 0 :(得分:1)

CodeIgniter's Email Library只是PHP的mail()

的包装器

如果你深入研究核心并检查system/libraries/Email.php_send_with_mail()就是启动PHP原生mail()的功能。

安全模式下的当前实现使用:

mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str)

没有安全模式的当前实施使用:

mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f ".$this->clean_email($this->_headers['From'])

这两者都不允许用户定义的内容填充$additional_parameters变量。您需要扩展CI_Email类才能完成此任务:

application/libraries/中创建一个包含内容的文件MY_Email.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Email extends CI_Email{
    public $additional_params = "";
    function __construct(){
        parent::__construct();
    }

    function additional_params($ap){
        $this->additional_params = $ap;
    }

    protected function _send_with_mail(){
        if ($this->_safe_mode == TRUE){
            if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, $this->additional_params)) return FALSE;
            else return TRUE;
        }
        else{
                if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f ".$this->clean_email($this->_headers['From']) . " " . $this->additional_params)) return FALSE;
                else return TRUE;
        }
    }    


}

现在,使用它:

$this->email->additional_params('something_goes_here');

当您致电$this->email->send()时,此功能将接管并附加参数!

尚未对此进行测试。但这是一般的想法。希望它有所帮助。

答案 1 :(得分:0)

随意使用php mail()函数。框架并不意味着您必须使用框架中的每个函数。