A modern, lightweight, and secure contact form built with PHP and Bootstrap 5 — powered by AJAX, PHPMailer, and Google reCAPTCHA v3, with zero jQuery dependency
- PHP 8.4+ support
- Bootstrap 5 UI – Responsive and clean
- AJAX-based form submission – No page reloads
- Google reCAPTCHA v3 – Prevent spam without hassle
- PHPMailer with SMTP Auth – Secure email delivery
- Client-side + Server-side Validation – Double-layered validation
- Bot Protection – Includes honeypot field to trap bots
- Clone or download the repository :
git clone https://github.com/raspgot/Contact-Form-PHP.git
- Run it locally with a local server like XAMPP
Before deploying, update your SMTP and reCAPTCHA credentials
- Get your reCAPTCHA keys from Google reCAPTCHA Admin
- Then, update the following constants in
AjaxForm.php
:
const SMTP_HOST = 'your.smtp.com';
const SMTP_USERNAME = 'your@email.com';
const SMTP_PASSWORD = 'yourpassword';
const SECRET_KEY = 'your_recaptcha_secret_key';
ℹ️ Ensure
php_curl
is enabled in yourphp.ini
:extension=curl
Replace the site key in AjaxForm.js
:
const RECAPTCHA_SITE_KEY = 'YOUR_RECAPTCHA_SITE_KEY';
- Add the reCAPTCHA script to
index.html
(just before</body>
) :
<script src="https://www.google.com/recaptcha/api.js?render=YOUR_RECAPTCHA_SITE_KEY"></script>
You can customize the success and error messages in the HTML :
<div class="valid-feedback">Looks good !</div>
<div class="invalid-feedback">Please provide a valid name.</div>
To add custom fields (e.g. subject, phone number) :
- Add them to
index.html
:
<input type="text" name="subject" class="form-control" required />
- Handle them in
AjaxForm.php
:
$subject = sanitize($_POST['subject']) ?? '';
- Include them in the email body and validation
Got a bug, idea, or suggestion? Contributions are very welcome !
Developed with ❤️ by Raspgot
If you found this project helpful, ⭐️ star the repo !