CodeIgniter rename email attachment file

I have a CodeIgniter script to send email with attachments.

$this->ci->email->attach("/path/to/file/myjhxvbXhjdSycv1y.pdf"); 

It works fine, but I have no idea how to rename the attached file to a more user-friendly line?

+4
source share
1 answer

CodeIgniter v3.x

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 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; // Can also be 'inline' Not sure if it matters return $this; } 

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'); 
+11
source

Source: https://habr.com/ru/post/1495968/


All Articles