添付ファイル付きの電子メールを送信するための CodeIgniter スクリプトがあります。
$this->ci->email->attach("/path/to/file/myjhxvbXhjdSycv1y.pdf");
うまく機能しますが、添付ファイルの名前をよりユーザーフレンドリーな文字列に変更する方法がわかりません。
添付ファイル付きの電子メールを送信するための CodeIgniter スクリプトがあります。
$this->ci->email->attach("/path/to/file/myjhxvbXhjdSycv1y.pdf");
うまく機能しますが、添付ファイルの名前をよりユーザーフレンドリーな文字列に変更する方法がわかりません。
この機能は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 = '')
ユーザーガイドによると:
カスタム ファイル名を使用する場合は、3 番目のパラメーターを使用できます。
$this->email->attach('filename.pdf', 'attachment', 'report.pdf');
ただし、CodeIgniter v2.x の場合、Emailライブラリを拡張してそれを実装できます。
system/libraries/Email.phpて中に入れるapplication/libraries/MY_ファイルの名前を変更し、プレフィックス (または で設定したものconfig.php)を追加します。application/libraries/MY_Email.php最初:これを72行目に挿入します。
var $_attach_new_name = array();
2 番目:行番号 161~ 166のコードを次のように変更します。
if ($clear_attachments !== FALSE)
{
    $this->_attach_new_name = array();
    $this->_attach_name     = array();
    $this->_attach_type     = array();
    $this->_attach_disp     = array();
}
3 番目: 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;
}
4 番目:最後に、1143行目でコードを次のように変更します。
$basename = ($this->_attach_new_name[$i] === NULL)
    ? basename($filename) : $this->_attach_new_name[$i];
$this->email->attach('/path/to/fileName.ext', 'attachment', 'newFileName.ext');