Skip to content

Commit 1d5d4a5

Browse files
committed
fix: email action renaming files maes them unsaved (closes #148)
1 parent 8293699 commit 1d5d4a5

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

classes/Actions/EmailAction.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ public function from(): User
205205
public function run(): void
206206
{
207207
try {
208+
208209
$email = App::instance()->email([
209210
'template' => $this->template(),
210211
'from' => $this->from(),
@@ -218,7 +219,19 @@ public function run(): void
218219
'submission' => $this->submission(),
219220
'form' => $this->submission()->form(),
220221
],
221-
'attachments' => $this->attachments()
222+
'attachments' => [], // don't pass attachments here, add them in beforeSend
223+
'beforeSend' => function ($mailer) {
224+
// add attachments with custom names
225+
foreach ($this->attachments() as $attachment) {
226+
if (is_array($attachment)) {
227+
$mailer->addAttachment($attachment['path'], $attachment['name']);
228+
} else {
229+
$mailer->addAttachment($attachment);
230+
}
231+
}
232+
233+
return $mailer;
234+
}
222235
]);
223236

224237
$this->log([
@@ -253,14 +266,10 @@ protected function attachments(): array
253266
} else { // is PHP file object
254267
$files = array_values(A::filter($value->value(), fn ($file) => $file['error'] === UPLOAD_ERR_OK));
255268
foreach ($files as $file) {
256-
$tmpName = pathinfo($file['tmp_name']);
257-
$filename = $tmpName['dirname'] . '/' . F::safeName($file['name']);
258-
259-
if (!F::exists($filename)) {
260-
rename($file['tmp_name'], $filename);
261-
}
262-
263-
$attachments[] = $filename;
269+
$attachments[] = [
270+
'path' => $file['tmp_name'],
271+
'name' => F::safeName($file['name'])
272+
];
264273
}
265274
}
266275
}

0 commit comments

Comments
 (0)