110 lines
2.9 KiB
PHP
110 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Mail;
|
|
|
|
use App\Enums\Listen\ObjectEnum;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Mail\Mailable;
|
|
use Illuminate\Mail\Mailables\Attachment;
|
|
use Illuminate\Mail\Mailables\Content;
|
|
use Illuminate\Mail\Mailables\Envelope;
|
|
use Illuminate\Mail\Markdown;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\Blade;
|
|
use Illuminate\Support\Str;
|
|
|
|
class SendContactMail extends Mailable
|
|
{
|
|
use Queueable;
|
|
use SerializesModels;
|
|
|
|
/**
|
|
* @var \stdClass
|
|
*/
|
|
private \stdClass $data;
|
|
|
|
/**
|
|
* Create a new message instance.
|
|
*
|
|
* @param \stdClass $data
|
|
* @return void
|
|
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
|
|
*/
|
|
public function __construct(\stdClass $data)
|
|
{
|
|
$this->data = clone $data;
|
|
$this->sanitizeStringInputs();
|
|
}
|
|
|
|
/**
|
|
* Sanitizes user message.
|
|
*
|
|
* ! This is not recursive !
|
|
* * Prevent xss injection via markdown interpretation.
|
|
*
|
|
* La solution devrai etre de filtrer en sortie, mais il n'est pas possible de
|
|
*
|
|
* @return void
|
|
*/
|
|
private function sanitizeStringInputs(): void
|
|
{
|
|
$this->data = (object) Arr::map((array) $this->data, function ($el) {
|
|
if (is_string($el)) {
|
|
$el = \htmlspecialchars(\strip_tags(Blade::render(
|
|
(string) Markdown::parse($el)
|
|
)));
|
|
$el = Str::of($el)->replaceMatches(
|
|
'/[^\p{L}0-9\n\s\@\.\:\-\_\?\!\;\(\)\+\-\/\\\=]+/us',
|
|
''
|
|
)->trim()->wordWrap(75, "\n", true)->toString();
|
|
}
|
|
return $el;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get the message envelope.
|
|
*
|
|
* @return \Illuminate\Mail\Mailables\Envelope
|
|
*/
|
|
public function envelope(): \Illuminate\Mail\Mailables\Envelope
|
|
{
|
|
return new Envelope(
|
|
subject: sprintf(
|
|
'%s - %s',
|
|
ObjectEnum::make($this->data->object)->label(),
|
|
config('app.name')
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get the message content definition.
|
|
*
|
|
* @return \Illuminate\Mail\Mailables\Content
|
|
*/
|
|
public function content(): \Illuminate\Mail\Mailables\Content
|
|
{
|
|
return new Content(
|
|
markdown: 'emails.send-contact-form',
|
|
with: ['data' => $this->data],
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get the attachments for the message.
|
|
*
|
|
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
|
|
*/
|
|
public function attachments(): array
|
|
{
|
|
$attachments = [];
|
|
if (isset($this->data->file)) {
|
|
$attachments[] = Attachment::fromPath($this->data->file->getRealPath())
|
|
->as($this->data->file->getClientOriginalName())
|
|
->withMime($this->data->file->getClientMimeType());
|
|
}
|
|
return $attachments;
|
|
}
|
|
}
|