CodeIgniter重命名电子邮件附件文件 [英] CodeIgniter rename email attachment file
问题描述
我有用于发送包含附件的电子邮件的CodeIgniter脚本。
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?
推荐答案
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 = '')
根据用户指南:
如果要使用自定义文件名,可以使用第三
参数:
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
但是对于CodeIgniter v2.x,您可以扩展电子邮件
库来实现:
- 创建
system / libraries / Email.php
的副本,并将其放在application / libraries /
- 重命名文件并添加
MY _
config.php
)application / libraries / MY_Email.php
- 打开文件并更改以下内容:
- Create a copy of
system/libraries/Email.php
and put it insideapplication/libraries/
- Rename the file and add
MY_
prefix (or whatever you have set inconfig.php
)application/libraries/MY_Email.php
- Open the file and change the following:
首先:在 #72 :
First: Insert this at line #72:
var $_attach_new_name = array();
第二次:更改 #161-166 : p>
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();
}
第三: > attach()函数 #409 并将其更改为:
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;
}
第四: a href =https://github.com/bcit-ci/CodeIgniter/blob/2.2.6/system/libraries/Email.php#L1143>#1143 将代码更改为:
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');
这篇关于CodeIgniter重命名电子邮件附件文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!