105 lines
2.6 KiB
PHP
105 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Mail;
|
|
|
|
use App\Models\MailContactData;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Mail\Mailable;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use League\Csv\Writer;
|
|
use SplTempFileObject;
|
|
|
|
class ContactMail extends Mailable
|
|
{
|
|
use Queueable;
|
|
use SerializesModels;
|
|
|
|
/**
|
|
* The order instance.
|
|
*
|
|
* @var MailContactData
|
|
*/
|
|
protected $data;
|
|
|
|
/**
|
|
* The joined file name
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $joinedFileName;
|
|
|
|
/**
|
|
* Create a new message instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct(MailContactData $data)
|
|
{
|
|
$this->data = $data;
|
|
}
|
|
|
|
/**
|
|
* Build the message.
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function build()
|
|
{
|
|
$this->joinFileIfProvided();
|
|
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']
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Attach a joined file if given in data
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function joinFileIfProvided(): void
|
|
{
|
|
if ($this->data->joinFile) {
|
|
$this->joinedFileName = sprintf(
|
|
'LEGAULOIS_PASS_MESSAGE-PJ-%s.%s',
|
|
date("YmdHis"),
|
|
$this->data->joinFile->extension()
|
|
);
|
|
$this->attach($this->data->joinFile, [
|
|
'as' => $this->joinedFileName,
|
|
'mime' => $this->data->joinFile->getMimeType()
|
|
]);
|
|
$this->data->joinFile = $this->joinedFileName;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
protected function makeCsvFile(): string
|
|
{
|
|
// we create the CSV into memory
|
|
// FILENAME
|
|
$csv = Writer::createFromFileObject(new SplTempFileObject());
|
|
$csv->setDelimiter(';');
|
|
$csv->setEnclosure('"');
|
|
$csv->setOutputBOM(Writer::BOM_UTF8);
|
|
|
|
// we 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->getContent();
|
|
}
|
|
}
|