ldc-boutique-cnc/app/Mail/SendContactMail.php
2023-03-09 11:58:40 +01:00

86 lines
2 KiB
PHP

<?php
namespace App\Mail;
use App\Models\Customer\Contact;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\File;
class SendContactMail extends Mailable
{
use Queueable;
use SerializesModels;
/**
* The contact data
*
* @var \App\Models\Customer\Contact
*/
protected $contact;
/**
* The joined file name
*
* @var string
*/
protected $joinedFileName;
/**
* Create a new message instance.
*
* @param \App\Models\Customer\Contact $contact
* @return void
*/
public function __construct(Contact $contact)
{
$this->contact = $contact;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$data = $this->contact;
$this->joinFileIfProvided();
return $this->subject('Contact - ' . config('theme.site_name', 'Boutique') . " - " . $data->subject)
->from($data->email)
->markdown('back::emails.send-contact', compact('data'));
}
/**
* Attach a joined file if given in data
*
* @return void
* @throws \RuntimeException If the mimetype cannot be guessed.
*/
protected function joinFileIfProvided(): void
{
if ($this->contact->file and File::exists($this->contact->file)) {
$extension = File::guessExtension($this->contact->file) ??
File::extension($this->contact->file);
$mimeType = File::mimeType($this->contact->file);
if (!$mimeType) {
throw new \RuntimeException('Could not guess joined file mimetype');
}
$this->joinedFileName = sprintf(
'BOUTIQUE_MESSAGE-PJ-%s.%s',
date("YmdHis"),
$extension
);
$this->attach($this->contact->file, [
'as' => $this->joinedFileName,
'mime' => $mimeType
]);
}
}
}