Add mail attachments to registration mail

This commit is contained in:
philipp lang 2024-04-14 10:49:44 +02:00
parent 8b42a62b2d
commit a6a8983ae9
3 changed files with 40 additions and 3 deletions

View File

@ -5,11 +5,12 @@ namespace App\Form\Mails;
use App\Form\Data\FormConfigData;
use App\Form\Models\Participant;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Attachment;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
class ConfirmRegistrationMail extends Mailable
{
@ -56,10 +57,12 @@ class ConfirmRegistrationMail extends Mailable
/**
* Get the attachments for the message.
*
* @return array<int, mixed>
* @return array<int, Attachment>
*/
public function attachments()
{
return [];
return $this->participant->form->getMedia('mailattachments')
->map(fn ($media) => Attachment::fromStorageDisk($media->disk, $media->getPathRelativeToRoot()))
->all();
}
}

View File

@ -21,4 +21,18 @@ trait FakesMedia
->toMediaCollection($collection);
});
}
public function withDocument(string $collection, string $filename, string $content = ''): self
{
return $this->afterCreating(function (HasMedia $model) use ($filename, $collection, $content) {
$pathinfo = pathinfo($filename);
UploadedFile::fake()->create($filename, $content, 'application/pdf')->storeAs('media-library', $filename, 'temp');
$model->addMediaFromDisk('media-library/' . $filename, 'temp')
->usingName($pathinfo['filename'])
->usingFileName($pathinfo['basename'])
->toMediaCollection($collection);
});
}
}

View File

@ -54,4 +54,24 @@ class FormRegisterMailTest extends FormTestCase
$mail->assertSeeInText('* Vorname: Max');
$mail->assertSeeInText('* Volljährig: Ja');
}
public function testItAttachesMailAttachments(): void
{
$this->login()->loginNami()->withoutExceptionHandling();
$participant = Participant::factory()->for(
Form::factory()
->fields([
$this->textField('vorname')->name('Vorname')->specialType(SpecialType::FIRSTNAME),
$this->textField('nachname')->specialType(SpecialType::LASTNAME),
])
->withDocument('mailattachments', 'beispiel.pdf', 'content1')
->withDocument('mailattachments', 'beispiel2.pdf', 'content2')
)
->data(['vorname' => 'Max', 'nachname' => 'Muster'])
->create();
$mail = new ConfirmRegistrationMail($participant);
$mail->assertHasAttachedData('content1', 'beispiel.pdf', ['mime' => 'application/pdf']);
$mail->assertHasAttachedData('content2', 'beispiel2.pdf', ['mime' => 'application/pdf']);
}
}