This guide walks you through setting up Google reCAPTCHA v3 integration in MailBear to prevent spam and bot submissions.
- MailBear database mode enabled
- Google account for reCAPTCHA admin access
- Domain where MailBear will be deployed
- Visit Google reCAPTCHA Admin Console
- Click "Create" to add a new site
- Fill in the form:
- Label: Choose a descriptive name (e.g., "MailBear - Production")
- reCAPTCHA type: Select "reCAPTCHA v3"
- Domains: Add your domain(s) where MailBear will be accessed
- For local development:
localhost - For production:
yourdomain.com
- For local development:
- Owners: Add additional Google accounts if needed
- Accept the Terms of Service
- Click "Submit"
- Copy your Site Key and Secret Key
Run the migration to add reCAPTCHA fields to your database:
# Option 1: Using Python script
python migrations/001_add_recaptcha_fields.py
# Option 2: Using SQL directly
mysql -u username -p database_name < migrations/001_add_recaptcha_fields.sqlChoose one of the following configuration methods:
Add the reCAPTCHA section to your config.yml:
recaptcha:
enabled: true
site_key: "6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI" # Replace with your site key
secret_key: "6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe" # Replace with your secret key
default_threshold: 0.5
timeout: 10Set the following environment variables:
export RECAPTCHA_ENABLED=true
export RECAPTCHA_SITE_KEY="your_site_key_here"
export RECAPTCHA_SECRET_KEY="your_secret_key_here"
export RECAPTCHA_DEFAULT_THRESHOLD=0.5
export RECAPTCHA_TIMEOUT=10After updating the configuration, restart the MailBear application:
# If running directly
python main.py
# If using Docker
docker-compose restartIf you configured reCAPTCHA globally, it will be available for all forms but disabled by default.
- Access the MailBear dashboard at
http://localhost:1234/forms - Create a new form or edit an existing one
- In the form editor, find the "reCAPTCHA Protection" section
- Check the "Enable reCAPTCHA Protection" checkbox
- (Optional) Enter form-specific Site Key and Secret Key if different from global config
- Set the threshold (recommended: 0.5)
- Save the form
The threshold determines how strict the reCAPTCHA verification is:
- 0.0: Accept all traffic (including bots)
- 0.1-0.3: Very permissive (may allow some sophisticated bots)
- 0.4-0.6: Balanced (recommended for most use cases)
- 0.7-0.9: Strict (may block some legitimate users)
- 1.0: Very strict (may block many legitimate users)
- Contact forms: 0.5
- Newsletter signup: 0.3
- Account registration: 0.7
- Payment forms: 0.8
- Admin forms: 0.9
- Create a test form with reCAPTCHA enabled
- Embed the form on a webpage
- Submit the form normally
- Check the logs for reCAPTCHA verification messages
- Set a very high threshold (0.9) temporarily
- Submit the form multiple times rapidly
- Some submissions should be blocked
- Reset to your desired threshold
- Temporarily use an invalid secret key
- Submit the form
- Verify error handling works correctly
- Restore the correct secret key
Possible causes:
- Invalid Site Key or Secret Key
- Domain not registered with reCAPTCHA
- Network connectivity issues
- API timeout
Solutions:
- Verify your keys in the Google reCAPTCHA console
- Check that your domain is listed in the reCAPTCHA site configuration
- Test with localhost for development
- Increase the timeout value
Possible causes:
- JavaScript errors on the page
- Content Security Policy blocking reCAPTCHA
- Network issues loading Google's scripts
Solutions:
- Check browser console for JavaScript errors
- Update CSP headers to allow reCAPTCHA domains
- Test on different networks/devices
Possible causes:
- Threshold set too high
- Legitimate users being blocked
Solutions:
- Lower the threshold gradually
- Monitor form submission patterns
- Consider different thresholds for different forms
Possible causes:
- Threshold set too low
- Advanced bots bypassing reCAPTCHA
Solutions:
- Increase the threshold
- Enable honeypot protection as well
- Monitor submission patterns for suspicious activity
To enable debug logging for reCAPTCHA:
- Set
debug: truein your config.yml - Restart MailBear
- Check logs for detailed reCAPTCHA verification information
Look for these log entries:
INFO: reCAPTCHA verification successful for form abc123 (score: 0.9)
WARNING: reCAPTCHA verification failed for form abc123: [error details]
ERROR: No reCAPTCHA secret key configured for form abc123
- Keep Secret Keys Private: Never commit secret keys to version control
- Use Environment Variables: Store sensitive configuration in environment variables
- Rotate Keys Regularly: Consider rotating reCAPTCHA keys periodically
- Monitor Logs: Regularly check logs for suspicious activity
- Combine with Honeypot: Use reCAPTCHA alongside honeypot protection for better coverage
- Test Regularly: Periodically test your reCAPTCHA integration
You can customize error messages in the JavaScript template by modifying the form handler.
For multi-tenant applications, you can configure different reCAPTCHA keys per form:
- Leave global configuration empty or disabled
- Configure each form individually with its own keys
- Use different thresholds based on form sensitivity
Monitor reCAPTCHA effectiveness:
- Track submission success rates
- Monitor score distributions
- Adjust thresholds based on data
For issues specific to reCAPTCHA integration:
- Check the troubleshooting section above
- Review MailBear logs for error messages
- Verify your reCAPTCHA configuration in Google's console
- Test with the provided test script:
python test_recaptcha_simple.py
For Google reCAPTCHA issues: