在 CakePHP 中将多个文件附加到电子邮件

Attach multiple files to email in CakePHP

我想在邮件中附加 5 个动态文本文件,但无法正常工作。

我在电子邮件中发送单个附件工作完美我的代码是:

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

尝试

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

documentation 中所述,attachments 方法将数组作为第一个参数,因此您的代码应如下所示:-

$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。您的代码无法正常工作,因为该方法没有采用多个参数。

为多个附件尝试此代码:

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

        ));