CodeIgniter rename email attachment file
Asked Answered
G

2

6

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?

Goodell answered 8/8, 2013 at 14:9 Comment(4)
rename() the file on the server before attaching the file.Vladikavkaz
i can't rename it on server dir. It must be do "on-fly" closely before sendAten
You would have to modify CI's email library to get this to work. It's not possible with the library as it stands, although it's a fairly easy modification.Grandiloquent
Great, it works, thak you for your help!Aten
V
16

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');
Vladikavkaz answered 8/8, 2013 at 15:31 Comment(1)
Hashem's answer was great but you would have to override to core Email.php library so there's no need to prefix the copy with MY_Shinn
E
1

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;
}  
Emphasize answered 8/6, 2021 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.