CodeIgniter重命名电子邮件附件文件

Jiř*_*šek 4 php email codeigniter codeigniter-2

我有CodeIgniter脚本用于发送带附件的电子邮件.

$this->ci->email->attach("/path/to/file/myjhxvbXhjdSycv1y.pdf");
Run Code Online (Sandbox Code Playgroud)

它工作得很好,但我不知道,如何将附加文件重命名为一些用户友好的字符串?

Has*_*ami 12

CodeIgniter v3.x

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 = '')
Run Code Online (Sandbox Code Playgroud)

根据用户指南:

如果您想使用自定义文件名,可以使用第三个参数:

$this->email->attach('filename.pdf', 'attachment', 'report.pdf');


CodeIgniter v2.x

但是对于CodeIgniter v2.x,您可以扩展Email库以实现:

  1. 创建一个副本system/libraries/Email.php并将其放入其中application/libraries/
  2. 重命名文件并添加MY_前缀(或您设置的任何内容config.php)application/libraries/MY_Email.php
  3. 打开文件并更改以下内容:

第一:#72行插入:

var $_attach_new_name = array();
Run Code Online (Sandbox Code Playgroud)

第二步:将第#161-166行的代码更改为:

if ($clear_attachments !== FALSE)
{
    $this->_attach_new_name = array();
    $this->_attach_name     = array();
    $this->_attach_type     = array();
    $this->_attach_disp     = array();
}
Run Code Online (Sandbox Code Playgroud)

第三步:找到#409attach()行的功能并将其更改为:

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;
}
Run Code Online (Sandbox Code Playgroud)

第四:最后在#1143行将代码更改为:

$basename = ($this->_attach_new_name[$i] === NULL)
    ? basename($filename) : $this->_attach_new_name[$i];
Run Code Online (Sandbox Code Playgroud)

用法

$this->email->attach('/path/to/fileName.ext', 'attachment', 'newFileName.ext');
Run Code Online (Sandbox Code Playgroud)