PHP CI框架email类发送邮件

用CI框架发送邮件类 在中文标题太长的情况下会出现乱码,搜索后说是发送邮件的时候有标题长度的限制,按说的方法修改后,还是没能得到解决,后来发现需要转换邮件标题的编码,解决方法如下:

打开 libraries/Email.php  找到:function subject($subject)

public function subject($subject)
{
$subject = $this->_prep_q_encoding($subject);
$this->_set_header('Subject', $subject);
return $this;
}

修改为

public function subject($subject)
{
//$subject = $this->_prep_q_encoding($subject);
$subject = '=?'. $this->charset .'?B?'. base64_encode($subject) .'?=';
$this->_set_header('Subject', $subject);
return $this;
}

代码为

function mail(){
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_user'] = 'XXXXXXXXXX@gmail.com';
$config['smtp_pass'] = 'XXXXXXXXXXXXX';
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '5';
$config['newline'] = "\r\n";
$config['crlf'] = "\r\n";
$this->email->initialize($config); $this->email->from('XXXXXXXXXX@gmail.com');
$this->email->to('123123123123@qq.com');
$this->email->subject("北极bear北极bear北极bear北极bear北极bear北极bear北极bear北极bear北极bear北极bear北极bear");
$this->email->message('哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈');
$this->email->send(); echo $this->email->print_debugger();
}

上面的解决方案,我将CI默认的对邮件标题(Subject)的编码方式从 Q-Encoding (简称 Q编码)变成了 Base64 (简称 B编码),希望能对大家有所帮助。如有问题欢迎留言讨论。

转自http://www.it300.com/article-8047.html

上一篇:改善你的jQuery的25个步骤 千倍级效率提升


下一篇:js中各种跨域问题实战小结(一)