The issue you're encountering is due to the way the attachments are being passed to the BulkEventEmail mailable. The buildAttachments method in the Mailable class expects each attachment to be an array with file and options keys, but your attachments are being passed as a simple array of file paths.
To resolve this, you need to structure your attachments correctly before passing them to the mailable. Here's how you can modify your submit method to achieve this:
- Ensure each attachment is an array with the required keys.
- Pass the structured attachments to the
BulkEventEmailmailable.
Here's the updated code:
public function submit()
{
$subjectLine = $this->form->getState()['subject'];
$emailBody = $this->form->getState()['body'];
$attachments = $this->form->getState()['attachments'];
// Structure the attachments correctly
$structuredAttachments = array_map(function ($attachment) {
return [
'file' => $attachment,
'options' => [],
];
}, $attachments);
$emailAddresses = $this->records['emails'];
foreach ($emailAddresses as $emailAddress) {
Mail::to($emailAddress)->send(new BulkEventEmail($subjectLine, $emailBody, $structuredAttachments));
}
}
In your BulkEventEmail mailable, you should handle the attachments accordingly. Here's an example of how you might do that:
class BulkEventEmail extends Mailable
{
public $subjectLine;
public $emailBody;
public $attachments;
public function __construct($subjectLine, $emailBody, $attachments)
{
$this->subjectLine = $subjectLine;
$this->emailBody = $emailBody;
$this->attachments = $attachments;
}
public function build()
{
$email = $this->view('emails.bulk_event')
->subject($this->subjectLine)
->with([
'body' => $this->emailBody,
]);
// Attach the files
foreach ($this->attachments as $attachment) {
$email->attach($attachment['file'], $attachment['options']);
}
return $email;
}
}
This should ensure that your attachments are correctly structured and passed to the mailable, avoiding the "Cannot access offset of type string on string" error.