Skip to content

Commit 189c2bf

Browse files
authored
fix(email): pass attachments to beforeSend closure (#150)
1 parent cf7e47a commit 189c2bf

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

classes/Actions/EmailAction.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ public function from(): User
205205
public function run(): void
206206
{
207207
try {
208+
// get attachments before email is created
209+
$attachments = $this->attachments();
208210

209211
$email = App::instance()->email([
210212
'template' => $this->template(),
@@ -219,10 +221,10 @@ public function run(): void
219221
'submission' => $this->submission(),
220222
'form' => $this->submission()->form(),
221223
],
222-
'attachments' => [], // don't pass attachments here, add them in beforeSend
223-
'beforeSend' => function ($mailer) {
224+
'attachments' => [], // attachments added in beforeSend for custom naming
225+
'beforeSend' => function ($mailer) use ($attachments) { // use captured attachments to ensure files are accessed at the correct time
224226
// add attachments with custom names
225-
foreach ($this->attachments() as $attachment) {
227+
foreach ($attachments as $attachment) {
226228
if (is_array($attachment)) {
227229
$mailer->addAttachment($attachment['path'], $attachment['name']);
228230
} else {
@@ -264,7 +266,7 @@ protected function attachments(): array
264266
$attachments[] = $file;
265267
}
266268
} else { // is PHP file object
267-
$files = array_values(A::filter($value->value(), fn ($file) => $file['error'] === UPLOAD_ERR_OK));
269+
$files = array_values(A::filter($value->value(), fn($file) => $file['error'] === UPLOAD_ERR_OK));
268270
foreach ($files as $file) {
269271
$attachments[] = [
270272
'path' => $file['tmp_name'],

0 commit comments

Comments
 (0)