72 lines
2.6 KiB
PHP
72 lines
2.6 KiB
PHP
<?php
|
|
namespace App\Livewire;
|
|
use App\Models\ContactEnquiry;
|
|
use Illuminate\Mail\Message;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Livewire\Component;
|
|
use Throwable;
|
|
class ContactForm extends Component {
|
|
public string $name = '';
|
|
public string $email = '';
|
|
public string $company = '';
|
|
public string $service = '';
|
|
public string $message = '';
|
|
public string $website = '';
|
|
public bool $submitted = false;
|
|
/** Save a valid enquiry before attempting its email notification. */
|
|
public function submit(): void
|
|
{
|
|
$this->validate(['name'=>'required|string|max:120','email'=>'required|email|max:255','company'=>'nullable|string|max:120',
|
|
'service'=>'nullable|string|max:100','message'=>'required|string|min:10|max:5000','website'=>'nullable|max:0']);
|
|
$enquiry = ContactEnquiry::create($this->only(['name','email','company','service','message']));
|
|
$this->reset(['name','email','company','service','message']);
|
|
$this->submitted = true;
|
|
|
|
$this->notifyByEmail($enquiry);
|
|
}
|
|
|
|
/** Send a notification and record failures without including enquiry contents. */
|
|
private function notifyByEmail(ContactEnquiry $enquiry): void
|
|
{
|
|
$recipient = config('mail.contact_recipient');
|
|
$context = ['enquiry_id' => $enquiry->getKey()];
|
|
|
|
if (! is_string($recipient) || ! filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
|
|
Log::warning('Contact enquiry notification skipped: configure CONTACT_ENQUIRY_TO.', $context);
|
|
|
|
return;
|
|
}
|
|
|
|
$body = implode("\n", [
|
|
"Enquiry #{$enquiry->getKey()}",
|
|
"Name: {$enquiry->name}",
|
|
"Email: {$enquiry->email}",
|
|
'Company: '.($enquiry->company ?: 'Not specified'),
|
|
'Service: '.($enquiry->service ?: 'Not specified'),
|
|
'',
|
|
$enquiry->message,
|
|
]);
|
|
|
|
try {
|
|
$sent = Mail::raw($body, function (Message $message) use ($recipient, $enquiry): void {
|
|
$message->to($recipient)
|
|
->replyTo($enquiry->email)
|
|
->subject('New ABSDEV contact enquiry');
|
|
});
|
|
} catch (Throwable $exception) {
|
|
Log::error('Contact enquiry notification failed.', $context + [
|
|
'exception_type' => $exception::class,
|
|
]);
|
|
|
|
return;
|
|
}
|
|
|
|
if ($sent === null) {
|
|
Log::warning('Contact enquiry notification was not sent.', $context);
|
|
}
|
|
}
|
|
|
|
public function render() { return view('livewire.contact-form'); }
|
|
}
|