CodeIgniter重命名电子邮件附件文件 [英] CodeIgniter rename email attachment file
问题描述
$ this-> ci - >的电子邮件 - >附加( /路径/到/文件/ myjhxvbXhjdSycv1y.pdf);
它的效果很好,但我不知道如何重命名附加文件到一些更加用户友好的字符串?
CodeIgniter v3.x
此功能已添加自 CI v3 :
/ **
*分配文件附件
*
* @param string $ file可以是本地路径,URL或缓冲内容
* @param string $ disposition ='attachment'
* @param string $ newname = NULL
* @param ($ file,$ disposition ='',$ newname = NULL,$ mime ='')
$ $ b
根据用户指南:
如果要使用自定义文件名,可以使用第三个
参数:
$ 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
- 打开文件并更改以下内容:
第一个:在 #72 :
var $ _attach_new_name = array();
第二个更改代码 #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 并将其更改为:
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; //也可以是'inline'不确定是否重要
return $ this;
}
第四:最后在 a href =https://github.com/bcit-ci/CodeIgniter/blob/2.2.6/system/libraries/Email.php#L1143 =noreferrer>#1143 将代码更改为:
$ basename =($ this-> _attach_new_name [$ i] === NULL)
? basename($ filename):$ this-> _attach_new_name [$ i];
用法
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
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:
- 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:
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');
这篇关于CodeIgniter重命名电子邮件附件文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!