Contact Form
A comprehensive full-stack contact form component with email functionality, admin dashboard, and message management built on top of AxionJS.
Contact Form
A full-stack contact form solution that provides form validation, email sending capabilities, database storage, and an admin dashboard for managing messages. Built with Next.js Server Actions and modern React patterns.

This component streamlines communication by capturing inquiries efficiently and providing tools for effective follow-up, all powered by Next.js Server Actions.
Features
Server Actions
Utilizes Next.js Server Actions for secure and efficient backend operations.
Dual Validation
Robust client-side and server-side validation using Zod for data integrity.
Email & DB Integration
Automated email notifications and persistent message storage in PostgreSQL.
Admin Email
Admins receive instant notifications for new messages, ensuring timely responses.
Installation
Install the Component
This command installs the contact form system, including frontend components, server actions, and database configurations.
Database Setup
The component uses Prisma. Add the following schema to your schema.prisma
file.
Libraries Used
This component leverages several modern libraries and frameworks:
- Next.js 14+: App Router with Server Actions
- React Hook Form: Form state management and validation
- Zod: Schema validation for both client and server
- Prisma: Database ORM for PostgreSQL
- Nodemailer: Email sending functionality
- Lucide React: Modern icon library
- date-fns: Date formatting utilities
- Tailwind CSS: Utility-first CSS framework
Usage
Embed the contact form in any page.
Component Structure
The contact form system consists of several interconnected components:
Core Components
- ContactForm: Main form component with validation
- MessageList: Admin interface for managing messages
- Admin Dashboard: Overview of message statistics
Hooks
- useContactForm: Form state management and submission logic
- useMessageList: Message management operations
Server Actions (actions/contact-actions.ts
)
- sendEmail: Handles form submission and email sending
- getMessages: Retrieves messages from database
- updateMessageStatus: Updates message read/unread status
- deleteMessage: Removes messages from database
- getUnreadMessageCounts: Gets count of unread messages
- getTotalMessageCount: Gets total message count
Customization
Customize form elements using Tailwind CSS.
Configuration Options
Form Behavior
You can customize form behavior by modifying the useContactForm
hook parameters.
Database Schema
Extend the ContactMessage
model in schema.prisma
as needed.
Remember to run npx prisma db push
after modifying your schema.
Admin Dashboard Features
Message Management
- Status Tracking: Mark messages as read/unread
- Filtering: Filter messages by status (all, unread, read)
- Bulk Operations: Delete multiple messages
- Search: Search through messages (can be extended)
Dashboard Metrics
The admin dashboard displays:
- Total message count
- Unread message count
- Recent activity overview
Error Handling
The component includes comprehensive error handling:
Client-Side Errors
Server-Side Errors
Security Considerations
Ensure proper security measures are in place for production use.
Email Security
- Use app-specific passwords for Gmail
- Store credentials securely in environment variables
- Implement rate limiting for form submissions
Data Validation
- Server-side validation is always performed
- Input sanitization prevents XSS attacks
- SQL injection protection via Prisma ORM
Access Control (Admin Routes)
Protect your admin dashboard using authentication.
Troubleshooting
Common Issues
Email not sending:
- Verify SMTP credentials
- Check email server settings
- Ensure app passwords are used for Gmail
Database connection issues:
- Verify DATABASE_URL format
- Check database permissions
- Run Prisma migrations
Form validation errors:
- Check Zod schema configuration
- Verify form field naming consistency
- Ensure proper error handling
Debugging
Enable debug logging:
API Reference
Server Actions
Prop | Type | Default |
---|---|---|
sendEmail | async (data: FormData) => Promise<{ success: boolean; error?: string }> | - |
getMessages | async () => Promise<ContactMessage[]> | - |
updateMessageStatus | async (id: string, status: string) => Promise<ContactMessage | { error: string }> | - |
deleteMessage | async (id: string) => Promise<{ success: boolean; error?: string }> | - |
getUnreadMessageCount | async () => Promise<number> | - |
Hooks
Prop | Type | Default |
---|---|---|
useContactForm() | () => { form: UseFormReturn; onSubmit: (data: FormValues) => Promise<void>; isPending: boolean; isSuccess: boolean; } | - |
useMessageList(initialMessages: Message[]) | (initialMessages: ContactMessage[]) => { /* ...return values... */ } | - |
This contact form component provides a complete solution for handling user inquiries with professional email integration and admin management capabilities. The modular design allows for easy customization while maintaining robust functionality.