From 033e90b85e84fc8f31b877dcd2e265c6d3c5e42f Mon Sep 17 00:00:00 2001 From: Mark Gregory Date: Thu, 19 Nov 2026 18:57:32 +0000 Subject: [PATCH] new design with nice layout --- .env.example | 1 + app/Livewire/ContactForm.php | 54 +++++++- config/mail.php | 2 + .../views/livewire/contact-form.blade.php | 2 +- .../ContactEnquiryNotificationTest.php | 115 ++++++++++++++++++ 5 files changed, 171 insertions(+), 3 deletions(-) create mode 100644 tests/Feature/ContactEnquiryNotificationTest.php diff --git a/.env.example b/.env.example index 40bb13b..9022fbc 100644 --- a/.env.example +++ b/.env.example @@ -55,6 +55,7 @@ MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_FROM_ADDRESS="hello@example.com" MAIL_FROM_NAME="${APP_NAME}" +CONTACT_ENQUIRY_TO= AWS_ACCESS_KEY_ID= AWS_SECRET_ACCESS_KEY= diff --git a/app/Livewire/ContactForm.php b/app/Livewire/ContactForm.php index 4266592..b718895 100644 --- a/app/Livewire/ContactForm.php +++ b/app/Livewire/ContactForm.php @@ -1,7 +1,11 @@ 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']); - ContactEnquiry::create($this->only(['name','email','company','service','message'])); + $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'); } } diff --git a/config/mail.php b/config/mail.php index e32e88d..fadaa20 100644 --- a/config/mail.php +++ b/config/mail.php @@ -16,6 +16,8 @@ return [ 'default' => env('MAIL_MAILER', 'log'), + 'contact_recipient' => env('CONTACT_ENQUIRY_TO'), + /* |-------------------------------------------------------------------------- | Mailer Configurations diff --git a/resources/views/livewire/contact-form.blade.php b/resources/views/livewire/contact-form.blade.php index dc35cbd..8f0c794 100644 --- a/resources/views/livewire/contact-form.blade.php +++ b/resources/views/livewire/contact-form.blade.php @@ -1,3 +1,3 @@
-@if($submitted)
Thank you — your enquiry has been received. We will get back to you.
@else +@if($submitted)
Thank you — your enquiry has been received.
@else
@error('name')

{{ $message }}

@enderror
@error('email')

{{ $message }}

@enderror
@error('message')

{{ $message }}

@enderror

Your details are only used to respond to your enquiry.

@endif
\ No newline at end of file diff --git a/tests/Feature/ContactEnquiryNotificationTest.php b/tests/Feature/ContactEnquiryNotificationTest.php new file mode 100644 index 0000000..c096046 --- /dev/null +++ b/tests/Feature/ContactEnquiryNotificationTest.php @@ -0,0 +1,115 @@ + 'enquiries@example.com', + 'mail.from.address' => 'site@example.com', + ]); + + $this->submitEnquiry(); + $this->assertDatabaseHas('contact_enquiries', [ + 'email' => 'visitor@example.com', + 'is_read' => false, + ]); + + $messages = Mail::mailer()->getSymfonyTransport()->messages(); + $this->assertCount(1, $messages); + $message = $messages->first()->getOriginalMessage(); + $this->assertSame('enquiries@example.com', $message->getTo()[0]->getAddress()); + $this->assertSame('site@example.com', $message->getFrom()[0]->getAddress()); + $this->assertSame('visitor@example.com', $message->getReplyTo()[0]->getAddress()); + $this->assertStringContainsString( + "Name: Test Visitor\nEmail: visitor@example.com\nCompany: Example Ltd\nService: Consultancy\n\nPlease tell me about Laravel support.", + $message->getTextBody(), + ); + } + + /** Verify that a transport failure keeps the enquiry and logs safe diagnostics. */ + public function test_transport_failure_preserves_enquiry_and_logs_its_id(): void + { + config(['mail.contact_recipient' => 'enquiries@example.com']); + Log::spy(); + $transport = Mockery::mock(TransportInterface::class); + $transport->shouldReceive('send')->once()->andReturnUsing(function (): never { + $this->assertDatabaseHas('contact_enquiries', ['email' => 'visitor@example.com']); + + throw new TransportException('Provider error with sensitive connection details.'); + }); + Mail::mailer()->setSymfonyTransport($transport); + + $this->submitEnquiry(); + + $enquiry = ContactEnquiry::sole(); + Log::shouldHaveReceived('error')->once()->with('Contact enquiry notification failed.', [ + 'enquiry_id' => $enquiry->getKey(), + 'exception_type' => TransportException::class, + ]); + } + + /** Verify that missing configuration is reported without sending a message. */ + public function test_missing_recipient_preserves_enquiry_and_logs_warning(): void + { + config(['mail.contact_recipient' => null]); + Log::spy(); + + $this->submitEnquiry(); + + $enquiry = ContactEnquiry::sole(); + $this->assertCount(0, Mail::mailer()->getSymfonyTransport()->messages()); + Log::shouldHaveReceived('warning')->once()->with( + 'Contact enquiry notification skipped: configure CONTACT_ENQUIRY_TO.', + ['enquiry_id' => $enquiry->getKey()], + ); + } + + /** Verify that validation rejects spam before saving or sending. */ + public function test_rejected_submission_sends_no_notification(): void + { + config(['mail.contact_recipient' => 'enquiries@example.com']); + + Livewire::test(ContactForm::class) + ->set('name', 'Test Visitor') + ->set('email', 'visitor@example.com') + ->set('message', 'Please tell me about Laravel support.') + ->set('website', 'spam') + ->call('submit') + ->assertHasErrors(['website']); + + $this->assertDatabaseCount('contact_enquiries', 0); + $this->assertCount(0, Mail::mailer()->getSymfonyTransport()->messages()); + } + + /** Submit valid data and verify the confirmation shown to the visitor. */ + private function submitEnquiry(): void + { + Livewire::test(ContactForm::class) + ->set('name', 'Test Visitor') + ->set('email', 'visitor@example.com') + ->set('company', 'Example Ltd') + ->set('service', 'Consultancy') + ->set('message', 'Please tell me about Laravel support.') + ->call('submit') + ->assertHasNoErrors() + ->assertSet('submitted', true) + ->assertSee('your enquiry has been received.'); + } +}