Getting a list of attachments from Swift_Message object
Asked Answered
D

2

6

As we can see below there isn't any method called getAttachments() so how can I list/get/loop thru the attachments from Swift_Message object? I couldn't find any info in documentation or an example. All the methods are listed below as follows and the doc is here. I can check everything else apart from attachments.

I'm using symfony to do this.

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
)

I can confirm that the object contains the attachments. Dump result:

toString() output

....
....
--_=_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() output

Swift_Message::__set_state(array(
   'headerSigners' => 
  array (
  ),
    ....
    ....
              array (
                'filename' => '38423961aa437473ae2d6cb8caf796d31e20dbc4.jpeg',
              ),
    ....
    ....
Dorella answered 10/12, 2015 at 17:9 Comment(0)
B
8

From the code it looks like attachments are added to the $_children (from Swift_Mime_SimpleMimeEntity) array..

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

    return $this;
}

That leads me to believe that you could get all of the attachments using ->getChildren().

Barbiturate answered 10/12, 2015 at 17:20 Comment(1)
That's the one. It is here: foreach ($message->getChildren() as $child) { $child->getHeaders()->get('content-disposition'); }. Thank you. I actually dumped that but didn't get any result so I guess I missed something.Dorella
D
2

Just wanted to give full example based on @qooplmao's "accepted" hint above so the complete solution is this:

$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;
}
Dorella answered 11/12, 2015 at 9:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.