【问题标题】:Attach multiple files to email in CakePHP在 CakePHP 中将多个文件附加到电子邮件
【发布时间】:2016-01-13 07:30:44
【问题描述】:

我想在邮件中附加 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');

【问题讨论】:

  • 您可能需要先仔细查看文档,然后检查如何使用 attachments() 方法。 ps,请始终提及您的确切 CakePHP 版本并相应地标记您的问题 - 谢谢。
  • 感谢您的宝贵意见。我的蛋糕php版本是2.3。
  • 我检查了附件文件文档,但没有定义如何发送多个附件。而且我的单个电子邮件附件工作正常。只是不工作多个附件。请帮助我。

标签: cakephp email-attachments cakephp-2.3


【解决方案1】:

试试这个代码的多个附件:

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

        ));

【讨论】:

  • 除非您遇到mimetype 的问题或想要重命名附加文件,否则此数组结构对于附加多个文件来说有点矫枉过正,并且创建的代码过多,因此更难维护。您只需将文件路径数组指定为shown here
【解决方案2】:

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。您的代码不起作用,因为该方法不采用多个参数。

【讨论】:

  • 多个附件不工作。单个附件工作正常。
  • 不知道为什么,因为它应该是这样工作的,也是我过去使用它的方式。
【解决方案3】:

试试

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 2014-06-25
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多