从Swift_Message对象获取附件列表

时间:2015-12-10 17:09:16

标签: php symfony swiftmailer

正如我们在下面看到的,没有任何名为getAttachments()的方法,那么如何列出/获取/循环来自Swift_Message对象的附件?我无法在文档或示例中找到任何信息。下面列出了所有方法和doc is here。我可以检查除附件之外的所有其他内容。

我使用symfony来做到这一点。

Array
(
    [0] => __construct
    [1] => newInstance
    [2] => addPart
    [3] => attachSigner
    [4] => detachSigner
    [5] => toString
    [6] => toByteStream
    [7] => __wakeup
    [8] => __clone
    [9] => getNestingLevel
    [10] => setSubject
    [11] => getSubject
    [12] => setDate
    [13] => getDate
    [14] => setReturnPath
    [15] => getReturnPath
    [16] => setSender
    [17] => getSender
    [18] => addFrom
    [19] => setFrom
    [20] => getFrom
    [21] => addReplyTo
    [22] => setReplyTo
    [23] => getReplyTo
    [24] => addTo
    [25] => setTo
    [26] => getTo
    [27] => addCc
    [28] => setCc
    [29] => getCc
    [30] => addBcc
    [31] => setBcc
    [32] => getBcc
    [33] => setPriority
    [34] => getPriority
    [35] => setReadReceiptTo
    [36] => getReadReceiptTo
    [37] => attach
    [38] => detach
    [39] => embed
    [40] => __toString
    [41] => setBody
    [42] => getCharset
    [43] => setCharset
    [44] => getFormat
    [45] => setFormat
    [46] => getDelSp
    [47] => setDelSp
    [48] => charsetChanged
    [49] => generateId
    [50] => getHeaders
    [51] => getContentType
    [52] => setContentType
    [53] => getId
    [54] => setId
    [55] => getDescription
    [56] => setDescription
    [57] => getMaxLineLength
    [58] => setMaxLineLength
    [59] => getChildren
    [60] => setChildren
    [61] => getBody
    [62] => getEncoder
    [63] => setEncoder
    [64] => getBoundary
    [65] => setBoundary
    [66] => encoderChanged
    [67] => __destruct
)

我可以确认该对象包含附件。转储结果:

toString()输出

....
....
--_=_swift_v4_1449763083_6fe131190f5a7df1b45687b6bbb117a9_=_
Content-Type: image/jpeg; name=38423961aa437473ae2d6cb8caf796d31e20dbc4.jpeg
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename=38423961aa437473ae2d6cb8caf796d31e20dbc4.jpeg

/9j/4AAQSkZJRgABAgEAYABgAAD/4RCIRXhpZgAATU0AKgAAAAgAA4dpAAQAAAABAAAIPpyeAAEA
....
....

var_export()输出

Swift_Message::__set_state(array(
   'headerSigners' => 
  array (
  ),
    ....
    ....
              array (
                'filename' => '38423961aa437473ae2d6cb8caf796d31e20dbc4.jpeg',
              ),
    ....
    ....

2 个答案:

答案 0 :(得分:7)

the code看,附件已添加到$_children(来自Swift_Mime_SimpleMimeEntity)数组中。

public function attach(Swift_Mime_MimeEntity $entity)
{
    $this->setChildren(array_merge($this->getChildren(), array($entity)));

    return $this;
}

这让我相信您可以使用->getChildren()获取所有附件。

答案 1 :(得分:2)

只是想根据@ qooplmao" s"接受"提供完整的例子。提示上面是完整的解决方案:

$message = This is your Swift_Message object

foreach ($message->getChildren() as $child) {
     echo str_replace('attachment; filename=', null, $child->getHeaders()->get('content-disposition')->getFieldBody()).PHP_EOL;
}