new design with nice layout
This commit is contained in:
@@ -55,6 +55,7 @@ MAIL_USERNAME=null
|
|||||||
MAIL_PASSWORD=null
|
MAIL_PASSWORD=null
|
||||||
MAIL_FROM_ADDRESS="[email protected]"
|
MAIL_FROM_ADDRESS="[email protected]"
|
||||||
MAIL_FROM_NAME="${APP_NAME}"
|
MAIL_FROM_NAME="${APP_NAME}"
|
||||||
|
CONTACT_ENQUIRY_TO=
|
||||||
|
|
||||||
AWS_ACCESS_KEY_ID=
|
AWS_ACCESS_KEY_ID=
|
||||||
AWS_SECRET_ACCESS_KEY=
|
AWS_SECRET_ACCESS_KEY=
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace App\Livewire;
|
namespace App\Livewire;
|
||||||
use App\Models\ContactEnquiry;
|
use App\Models\ContactEnquiry;
|
||||||
|
use Illuminate\Mail\Message;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Mail;
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
|
use Throwable;
|
||||||
class ContactForm extends Component {
|
class ContactForm extends Component {
|
||||||
public string $name = '';
|
public string $name = '';
|
||||||
public string $email = '';
|
public string $email = '';
|
||||||
@@ -10,12 +14,58 @@ class ContactForm extends Component {
|
|||||||
public string $message = '';
|
public string $message = '';
|
||||||
public string $website = '';
|
public string $website = '';
|
||||||
public bool $submitted = false;
|
public bool $submitted = false;
|
||||||
public function submit(): void {
|
/** 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',
|
$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']);
|
'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->reset(['name','email','company','service','message']);
|
||||||
$this->submitted = true;
|
$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'); }
|
public function render() { return view('livewire.contact-form'); }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ return [
|
|||||||
|
|
||||||
'default' => env('MAIL_MAILER', 'log'),
|
'default' => env('MAIL_MAILER', 'log'),
|
||||||
|
|
||||||
|
'contact_recipient' => env('CONTACT_ENQUIRY_TO'),
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Mailer Configurations
|
| Mailer Configurations
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
<div class="rounded-3xl border border-white/10 bg-white/[.035] p-7 md:p-10">
|
<div class="rounded-3xl border border-white/10 bg-white/[.035] p-7 md:p-10">
|
||||||
@if($submitted)<div class="rounded-xl border border-acid/30 bg-acid/10 px-5 py-4 text-acid" role="status">Thank you — your enquiry has been received. We will get back to you.</div>@else
|
@if($submitted)<div class="rounded-xl border border-acid/30 bg-acid/10 px-5 py-4 text-acid" role="status">Thank you — your enquiry has been received.</div>@else
|
||||||
<form wire:submit="submit" class="grid gap-6 md:grid-cols-2"><div><label class="field-label" for="contact-name">Your name *</label><input id="contact-name" wire:model="name" class="field-input" autocomplete="name" required>@error('name')<p class="field-error">{{ $message }}</p>@enderror</div><div><label class="field-label" for="contact-email">Email address *</label><input id="contact-email" wire:model="email" class="field-input" type="email" autocomplete="email" required>@error('email')<p class="field-error">{{ $message }}</p>@enderror</div><div><label class="field-label" for="contact-company">Company</label><input id="contact-company" wire:model="company" class="field-input" autocomplete="organization"></div><div><label class="field-label" for="contact-service">I'm interested in</label><select id="contact-service" wire:model="service" class="field-input"><option value="">Select a service</option><option>Web development</option><option>macOS / iOS app</option><option>Systems & integrations</option><option>UI / UX design</option><option>Consultancy</option><option>Other</option></select></div><div class="md:col-span-2"><label class="field-label" for="contact-message">Tell us about your project *</label><textarea id="contact-message" wire:model="message" class="field-input min-h-36" required minlength="10" maxlength="5000" placeholder="What are you looking to build?"></textarea>@error('message')<p class="field-error">{{ $message }}</p>@enderror</div><div class="hidden" aria-hidden="true"><label for="website">Website</label><input id="website" wire:model="website" tabindex="-1" autocomplete="off"></div><div class="md:col-span-2 flex flex-wrap items-center justify-between gap-4"><p class="text-xs text-slate-500">Your details are only used to respond to your enquiry.</p><button type="submit" class="button-acid" wire:loading.attr="disabled">Send enquiry <span>↗</span></button></div></form>@endif</div>
|
<form wire:submit="submit" class="grid gap-6 md:grid-cols-2"><div><label class="field-label" for="contact-name">Your name *</label><input id="contact-name" wire:model="name" class="field-input" autocomplete="name" required>@error('name')<p class="field-error">{{ $message }}</p>@enderror</div><div><label class="field-label" for="contact-email">Email address *</label><input id="contact-email" wire:model="email" class="field-input" type="email" autocomplete="email" required>@error('email')<p class="field-error">{{ $message }}</p>@enderror</div><div><label class="field-label" for="contact-company">Company</label><input id="contact-company" wire:model="company" class="field-input" autocomplete="organization"></div><div><label class="field-label" for="contact-service">I'm interested in</label><select id="contact-service" wire:model="service" class="field-input"><option value="">Select a service</option><option>Web development</option><option>macOS / iOS app</option><option>Systems & integrations</option><option>UI / UX design</option><option>Consultancy</option><option>Other</option></select></div><div class="md:col-span-2"><label class="field-label" for="contact-message">Tell us about your project *</label><textarea id="contact-message" wire:model="message" class="field-input min-h-36" required minlength="10" maxlength="5000" placeholder="What are you looking to build?"></textarea>@error('message')<p class="field-error">{{ $message }}</p>@enderror</div><div class="hidden" aria-hidden="true"><label for="website">Website</label><input id="website" wire:model="website" tabindex="-1" autocomplete="off"></div><div class="md:col-span-2 flex flex-wrap items-center justify-between gap-4"><p class="text-xs text-slate-500">Your details are only used to respond to your enquiry.</p><button type="submit" class="button-acid" wire:loading.attr="disabled">Send enquiry <span>↗</span></button></div></form>@endif</div>
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use App\Livewire\ContactForm;
|
||||||
|
use App\Models\ContactEnquiry;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Mail;
|
||||||
|
use Livewire\Livewire;
|
||||||
|
use Mockery;
|
||||||
|
use Symfony\Component\Mailer\Exception\TransportException;
|
||||||
|
use Symfony\Component\Mailer\Transport\TransportInterface;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class ContactEnquiryNotificationTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/** Verify the saved enquiry and the message handed to the mail transport. */
|
||||||
|
public function test_saves_enquiry_and_sends_to_configured_recipient(): void
|
||||||
|
{
|
||||||
|
config([
|
||||||
|
'mail.contact_recipient' => '[email protected]',
|
||||||
|
'mail.from.address' => '[email protected]',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->submitEnquiry();
|
||||||
|
$this->assertDatabaseHas('contact_enquiries', [
|
||||||
|
'email' => '[email protected]',
|
||||||
|
'is_read' => false,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$messages = Mail::mailer()->getSymfonyTransport()->messages();
|
||||||
|
$this->assertCount(1, $messages);
|
||||||
|
$message = $messages->first()->getOriginalMessage();
|
||||||
|
$this->assertSame('[email protected]', $message->getTo()[0]->getAddress());
|
||||||
|
$this->assertSame('[email protected]', $message->getFrom()[0]->getAddress());
|
||||||
|
$this->assertSame('[email protected]', $message->getReplyTo()[0]->getAddress());
|
||||||
|
$this->assertStringContainsString(
|
||||||
|
"Name: Test Visitor\nEmail: [email protected]\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' => '[email protected]']);
|
||||||
|
Log::spy();
|
||||||
|
$transport = Mockery::mock(TransportInterface::class);
|
||||||
|
$transport->shouldReceive('send')->once()->andReturnUsing(function (): never {
|
||||||
|
$this->assertDatabaseHas('contact_enquiries', ['email' => '[email protected]']);
|
||||||
|
|
||||||
|
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' => '[email protected]']);
|
||||||
|
|
||||||
|
Livewire::test(ContactForm::class)
|
||||||
|
->set('name', 'Test Visitor')
|
||||||
|
->set('email', '[email protected]')
|
||||||
|
->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', '[email protected]')
|
||||||
|
->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.');
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user