Technical article
Build a secure contact form in Laravel
Bound and validate Laravel inputs, protect browser flows and test mail failures and repeat submissions without real mail.
- Security & privacy
- Build
- secure
- contact
- form
- Laravel

In this article
A secure Laravel contact form accepts only bounded, validated inputs and gives an honest outcome when mail processing fails. Combine normal web middleware with abuse controls and prevent repeated submissions from silently generating multiple messages.
Define the server contract first
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:100'],
'email' => ['required', 'email:rfc', 'max:254'],
'message' => ['required', 'string', 'max:5000'],
'website' => ['nullable', 'string', 'max:0'], // honeypot
];
}This illustrative Form Request method treats name, email and message as business fields; website is an empty honeypot. Start from validated values, but do not pass protection fields into the mailable. Align required contact fields with the actual form contract.
From form to controlled processing
Use existing web middleware and CSRF protection for browser forms; add @csrf in Blade. Apply an appropriate throttle. Keep honeypots out of normal tab order and assistive technology. An unusually fast submission is only a signal, not reliable proof of human identity.
use App\Http\Requests\ContactRequest;
use App\Mail\PublicFormMail;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Mail;
final class ContactStoreController
{
public function __invoke(ContactRequest $request): RedirectResponse
{
$fields = $request->safe()->only(['name', 'email', 'message']);
Mail::to(config('mail.contact_recipient'))
->send(new PublicFormMail($fields));
return redirect()->route('contact.thank-you');
}
}The controller assumes an existing Form Request, mailable, recipient configuration and thank-you route. Synchronous sending can suit limited volume with manageable timeouts. Use queues only with actual processing and monitoring. A redirect after transport acceptance does not prove inbox receipt.
Sender and failure handling
Use a configured sender on your own domain and the validated visitor address only as Reply-To. Do not place arbitrary input directly into mail headers. Escape message content in templates. Transport failure needs a general, recoverable error preserving useful input without logging payloads or secrets.
Illustrative acceptance exercise without real mail
- Valid request: fake transport receives only intended business fields.
- Invalid input or populated honeypot: no mail.
- Repeated submission: check the agreed deduplication behaviour; disabling a button alone is insufficient.
- Provider failure: no misleading success; the user can recover.
- Reopening the thank-you page: no new mail and no new enquiry record.
An SMTP timeout can occur after the provider accepts a message. Do not promise exactly-once delivery from a simple retry; define request status and recovery explicitly.
Code examples are illustrative. They have not been executed in a complete Laravel project; adapt imports, relationships, routes and configuration to your project and test actual behaviour.
Last updated:


