Attach multiple files to email in CakePHP

不想你离开。 提交于 2020-01-17 18:09:10

问题


I want to attach 5 dynamic text file in mail but not working.

I send single attachment in email working perfect my code is :

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

But i send multiple attachment in email not working.

My code is :

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

回答1:


Try this code for multiple attachment :

$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'
            )

        ));



回答2:


As described in the documentation the attachments method takes an array as the first parameter so your code should look like:-

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

This is also clearly stated in the API documentation (always worth checking).

If you look at the actual code for attachments() you'll see that if you pass just a string for the first parameter it gets cast as an array. Your code isn't working as the method doesn't take multiple parameters.




回答3:


Try

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


来源:https://stackoverflow.com/questions/34760777/attach-multiple-files-to-email-in-cakephp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!