I have CodeIgniter script for sending email with attachments.
$this->ci->email->attach("/path/to/file/myjhxvbXhjdSycv1y.pdf");
It works great, but I have no idea, how rename attached file to some more user-friendly string?
This feature has been added since 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 = '')
According to the user guide:
If you’d like to use a custom file name, you can use the third parameter:
$this->email->attach('filename.pdf', 'attachment', 'report.pdf');
However for CodeIgniter v2.x, you can extend the Email
library to implement that:
system/libraries/Email.php
and put it inside application/libraries/
MY_
prefix (or whatever you have set in config.php
) application/libraries/MY_Email.php
First: Insert this at line #72:
var $_attach_new_name = array();
Second: Change the code at line #161-166 to:
if ($clear_attachments !== FALSE)
{
$this->_attach_new_name = array();
$this->_attach_name = array();
$this->_attach_type = array();
$this->_attach_disp = array();
}
Third: Find the attach()
function at line #409 and change it to:
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;
}
Fourth: Finally at line #1143 change the code to:
$basename = ($this->_attach_new_name[$i] === NULL)
? basename($filename) : $this->_attach_new_name[$i];
$this->email->attach('/path/to/fileName.ext', 'attachment', 'newFileName.ext');
This method is available also for CodeIgniter v1.7x
but you must not forget to modify also this function below to add $this->_attach_new_name
to be also cleaned:
public function clear($clear_attachments = FALSE){
$this->_subject = "";
$this->_body = "";
$this->_finalbody = "";
$this->_header_str = "";
$this->_replyto_flag = FALSE;
$this->_recipients = array();
$this->_cc_array = array();
$this->_bcc_array = array();
$this->_headers = array();
$this->_debug_msg = array();
$this->_set_header('User-Agent', $this->useragent);
$this->_set_header('Date', $this->_set_date());
if ($clear_attachments !== FALSE){
$this->_attach_new_name = array();
$this->_attach_name = array();
$this->_attach_type = array();
$this->_attach_disp = array();
}
return $this;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With