2016-01-13 101 views
-1

我想在郵件中附加5個動態文本文件但不工作。將多個文件附加到CakePHP中的電子郵件

我在郵件發送單個附件的工作完美我的代碼是:

$Email->attachments('path/to/example.txt'); 

但我在郵件發送多附件不能正常工作。

我的代碼是:

$Email->attachments('path/to/example.txt','path/to/example1.txt','path/to/example3.txt','abs/path/to/example4.txt','path/to/example5.txt'); 
+2

您可能希望在文檔細看第一,並檢查如何'附件()'方法是精神疾病中使用。 ps,請始終提及您的確切CakePHP版本並相應地標記您的問題 - 謝謝。 – ndm

+0

感謝您的寶貴意見。我的蛋糕php版本是2.3。 – devrider

+0

我檢查附件文件的文檔,但沒有定義如何發送多個attachment.And也我的單一電子郵件附件工作fine.only不工作多個附件。請幫助我。 – devrider

回答

0

嘗試使用多個連接驗證碼:

$Email->attachments(array(
      'example.txt' => array(
       'file' => 'path/to/example.txt', 
       'mimetype' => 'text/plain' 
      ), 
example1.txt' => array(
       'file' => 'path/to/example1.txt', 
       'mimetype' => 'text/plain' 
      ), 
example3.txt' => array(
       'file' => 'path/to/example3.txt', 
       'mimetype' => 'text/plain' 
      ), 
example4.txt' => array(
       'file' => 'path/to/example4.txt', 
       'mimetype' => 'text/plain' 
      ), 
example5.txt' => array(
       'file' => 'path/to/example5.txt', 
       'mimetype' => 'text/plain' 
      ) 

     )); 
+0

非常感謝@ashish – devrider

+0

除非您遇到'mimetype'問題或者想要重命名附加文件,否則這個數組結構對於附加多個文件並創建更多的代碼比創建更多的代碼有點矯枉過正,因此更難以維護。你只需要指定一個文件路徑的數組,如[這裏顯示](http://stackoverflow.com/a/34761362/311992)。 – drmonkeyninja

0

嘗試

$Email->attachments(
    array(
     'path/to/example.txt', 
     'path/to/example1.txt', 
     'path/to/example3.txt', 
     'abs/path/to/example4.txt', 
     'path/to/example5.txt' 
    ) 
); 
+0

不能正常工作.... !!!! – devrider

0

正如documentationattachments方法描述帶有一個數組作爲第一個參數,以便您的代碼如下所示: -

$Email->attachments(array(
    'path/to/example.txt', 
    'path/to/example1.txt', 
    'path/to/example3.txt', 
    'abs/path/to/example4.txt', 
    'path/to/example5.txt' 
)); 

這也清楚地陳述在API documentation (總是值得檢查)。

如果你看看attachments()的實際代碼,你會發現如果你只傳遞一個字符串作爲第一個參數,那麼它會被轉換爲array。你的代碼不工作,因爲該方法不需要多個參數。

+0

不適用於多個附件。適用於單個附件。 – devrider

+0

不知道爲什麼這是它應該如何工作,以及我過去如何使用它。 – drmonkeyninja