114 lines
2.7 KiB
PHP
114 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Mail;
|
|
|
|
use App\Models\MailContactData;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Mail\Attachment;
|
|
use Illuminate\Mail\Mailable;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use League\Csv\Bom;
|
|
use League\Csv\Writer;
|
|
use SplTempFileObject;
|
|
|
|
class ContactMail extends Mailable
|
|
{
|
|
use Queueable;
|
|
use SerializesModels;
|
|
|
|
/**
|
|
* The order instance.
|
|
*
|
|
* @var \App\Models\MailContactData
|
|
*/
|
|
protected $data;
|
|
|
|
/**
|
|
* The path of a specific file.
|
|
*
|
|
* @var string|null
|
|
*/
|
|
protected $file;
|
|
|
|
/**
|
|
* Create a new message instance.
|
|
*
|
|
* @param \App\Models\MailContactData $data
|
|
* @param string|null $file
|
|
* @return void
|
|
*/
|
|
public function __construct(MailContactData $data, string|null $file = null)
|
|
{
|
|
$this->data = $data;
|
|
$this->file = $file;
|
|
}
|
|
|
|
/**
|
|
* Build the message.
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function build()
|
|
{
|
|
return $this
|
|
->subject(sprintf('Nouveau message consommateur Le Gaulois (%s)', $this->data->type->label()))
|
|
->view('emails.contact')
|
|
->text('emails.contact-plain')
|
|
->with('data', $this->data)
|
|
->attachData(
|
|
$this->makeCsvFile(),
|
|
sprintf('contact_le_gaulois_%d.csv', time()),
|
|
['mime' => 'text/csv']
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get the attachments for the message.
|
|
*
|
|
* @return array<int, \Illuminate\Mail\Attachment>
|
|
*/
|
|
public function attachments(): array
|
|
{
|
|
if (!$this->file) {
|
|
return [];
|
|
}
|
|
return [
|
|
Attachment::fromPath($this->file)->as($this->data->joinFile),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
protected function makeCsvFile(): string
|
|
{
|
|
// Creation of the CSV into memory.
|
|
$csv = Writer::createFromFileObject(new SplTempFileObject());
|
|
$csv->setDelimiter(';');
|
|
$csv->setEnclosure('"');
|
|
$csv->setOutputBOM(Bom::Utf8);
|
|
|
|
// Insert the CSV header.
|
|
$csv->insertOne([
|
|
'EMETTEUR_DATE',
|
|
'EMETTEUR_HEURE',
|
|
'EMETTEUR_CIVILITE',
|
|
'EMETTEUR_NOM',
|
|
'EMETTEUR_PRENOM',
|
|
'EMETTEUR_ADRESSE',
|
|
'EMETTEUR_CP',
|
|
'EMETTEUR_VILLE',
|
|
'EMETTEUR_EMAIL',
|
|
'EMETTEUR_OBJETDEMANDE',
|
|
'EMETTEUR_MESSAGE',
|
|
'EMETTEUR_CATEGORIE',
|
|
'EMETTEUR_ACTIVITE',
|
|
'REPONSE_ETAT',
|
|
'TYPE_REPONSE'
|
|
]);
|
|
|
|
$csv->insertOne($this->data->exportToArcadiaDataArray());
|
|
|
|
return $csv->toString();
|
|
}
|
|
}
|