Skip to content

Commit 587dacf

Browse files
authored
Fixed attachment error
Fixed error Call to undefined method Swift_MimePart::get File name() in ElasticTransport.php (line 122)
1 parent 22af8e0 commit 587dacf

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

src/ElasticTransport.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null)
8080

8181
];
8282

83+
8384
$attachments = $message->getChildren();
84-
$count = count($attachments);
85-
if (is_array($attachments) && $count > 0) {
85+
$attachmentCount = $this->checkAttachmentCount($attachments);
86+
if ($attachmentCount > 0) {
8687
$data = $this->attach($attachments, $data);
8788
}
8889
$ch = curl_init();
@@ -99,8 +100,8 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null)
99100
$result = curl_exec($ch);
100101
curl_close($ch);
101102

102-
if ($count > 0) {
103-
$this->deleteTempAttachmentFiles($data, $count);
103+
if ($attachmentCount > 0) {
104+
$this->deleteTempAttachmentFiles($data, $attachmentCount);
104105
}
105106

106107
return $result;
@@ -118,22 +119,41 @@ public function attach($attachments, $data)
118119
if (is_array($attachments) && count($attachments) > 0) {
119120
$i = 1;
120121
foreach ($attachments AS $attachment) {
121-
$attachedFile = $attachment->getBody();
122-
$fileName = $attachment->getFilename();
123-
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
124-
$tempName = uniqid() . '.' . $ext;
125-
Storage::put($tempName, $attachedFile);
126-
$type = $attachment->getContentType();
127-
$attachedFilePath = storage_path('app\\' . $tempName);
128-
$data['file_' . $i] = new \CurlFile($attachedFilePath, $type, $fileName);
129-
$i++;
122+
if ($attachment instanceof \Swift_Attachment) {
123+
$attachedFile = $attachment->getBody();
124+
$fileName = $attachment->getFilename();
125+
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
126+
$tempName = uniqid() . '.' . $ext;
127+
Storage::put($tempName, $attachedFile);
128+
$type = $attachment->getContentType();
129+
$attachedFilePath = storage_path('app\\' . $tempName);
130+
$data['file_' . $i] = new \CurlFile($attachedFilePath, $type, $fileName);
131+
$i++;
132+
}
130133
}
131134
}
132135

133136
return $data;
134137
}
135138

136139

140+
/**
141+
* Check Swift_Attachment count
142+
* @param $attachments
143+
* @return bool
144+
*/
145+
public function checkAttachmentCount($attachments)
146+
{
147+
$count = 0;
148+
foreach ($attachments AS $attachment) {
149+
if ($attachment instanceof \Swift_Attachment) {
150+
$count++;
151+
}
152+
}
153+
return $count;
154+
}
155+
156+
137157
/**
138158
* Get the plain text part.
139159
*

0 commit comments

Comments
 (0)