CodeIgniter v3.x
从CI v3开始添加此功能:
/**
* Assign file attachments
*
* @param string $file Can be local path, URL or buffered content
* @param string $disposition = 'attachment'
* @param string $newname = NULL
* @param string $mime = ''
* @return CI_Email
*/
public function attach($file, $disposition = '', $newname = NULL, $mime = '')
根据user guide:
如果您想使用自定义文件名,可以使用第三个
参数:
$this->email->attach('filename.pdf', 'attachment', 'report.pdf');
CodeIgniter v2.x
但是对于 CodeIgniter v2.x,您可以扩展 Email 库来实现它:
- 创建
system/libraries/Email.php 的副本并将其放入application/libraries/
- 重命名文件并添加
MY_前缀(或您在config.php中设置的任何内容)application/libraries/MY_Email.php
- 打开文件并更改以下内容:
首先:在#72行插入:
var $_attach_new_name = array();
第二:将#161-166行的代码改为:
if ($clear_attachments !== FALSE)
{
$this->_attach_new_name = array();
$this->_attach_name = array();
$this->_attach_type = array();
$this->_attach_disp = array();
}
第三:在#409行找到attach()函数并将其更改为:
public function attach($filename, $disposition = 'attachment', $new_name = NULL)
{
$this->_attach_new_name[] = $new_name;
$this->_attach_name[] = $filename;
$this->_attach_type[] = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
$this->_attach_disp[] = $disposition; // Can also be 'inline' Not sure if it matters
return $this;
}
第四:最后在#1143行将代码更改为:
$basename = ($this->_attach_new_name[$i] === NULL)
? basename($filename) : $this->_attach_new_name[$i];
用法
$this->email->attach('/path/to/fileName.ext', 'attachment', 'newFileName.ext');