Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter rename email attachment file

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?

like image 986
Jiří Valoušek Avatar asked Aug 08 '13 14:08

Jiří Valoušek


2 Answers

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 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');


CodeIgniter v2.x

However for CodeIgniter v2.x, you can extend the Email library to implement that:

  1. Create a copy of system/libraries/Email.php and put it inside application/libraries/
  2. Rename the file and add MY_ prefix (or whatever you have set in config.php) application/libraries/MY_Email.php
  3. Open the file and change the following:

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];

Usage

$this->email->attach('/path/to/fileName.ext', 'attachment', 'newFileName.ext');
like image 67
Hashem Qolami Avatar answered Nov 09 '22 05:11

Hashem Qolami


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;
}  
like image 27
Mike Fr Avatar answered Nov 09 '22 04:11

Mike Fr