CodeIgniter v3.x
This feature has been added since CI v3 :
public function attach($file, $disposition = '', $newname = NULL, $mime = '')
According to user manual :
If you like to use the name of the custom file, you can use the third Parameter:
$this->email->attach('filename.pdf', 'attachment', 'report.pdf');
CodeIgniter v2.x
However, for CodeIgniter v2.x, you can extend the Email library to implement this:
- Create a copy of
system/libraries/Email.php and place it inside application/libraries/ - Rename the file and add the prefix
MY_ (or whatever you set in config.php ) application/libraries/MY_Email.php - Open the file and change the following:
First: Insert this into line # 72 :
var $_attach_new_name = array();
Second: Change the code in line # 161-166 :
if ($clear_attachments !== FALSE) { $this->_attach_new_name = array(); $this->_attach_name = array(); $this->_attach_type = array(); $this->_attach_disp = array(); }
Third: Locate the attach() function in line # 409 and change this:
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;
Fourth: Finally, on line # 1143, change the code to:
$basename = ($this->_attach_new_name[$i] === NULL) ? basename($filename) : $this->_attach_new_name[$i];
Using
$this->email->attach('/path/to/fileName.ext', 'attachment', 'newFileName.ext');