Contact Form Emails Not Sending? Try This First
Your PHP mail() function is probably blocked by the server. The real fix is SMTP authentication via a mail library.
Your Contact Form Emails Are Vanishing
I know the feeling — you spend hours building a contact form, hit send, and... nothing. No email. No error. Just dead silence. I've seen this on everything from shared hosting to AWS. The fix is usually simpler than you think.
The Real Fix: Ditch mail() and Use SMTP
Almost every modern web host blocks the PHP mail() function because spammers abuse it. Even if it works on your dev machine, production servers often have mail() disabled or rate-limited. The real fix is to send emails via SMTP with authentication.
Step 1: Check if mail() is blocked
Create a test file on your server:
<?php
phpinfo();
?>
Look for the sendmail_path directive. If it's empty or points to /usr/sbin/sendmail -t -i but still doesn't work, SMTP is your only reliable path.
Step 2: Install a mail library
For WordPress, use WP Mail SMTP or Post SMTP. For custom PHP, use PHPMailer or SwiftMailer. Here's a barebones PHPMailer example:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // your SMTP server
$mail->SMTPAuth = true;
$mail->Username = 'your.email@gmail.com';
$mail->Password = 'your-app-password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('you@example.com');
$mail->Subject = 'Test from SMTP';
$mail->Body = 'This works!';
$mail->send();
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
Step 3: Use an app password
If you're using Gmail, Outlook, or any provider with 2FA, you must generate an app password (not your regular password). Google calls them "App Passwords" — generate one in your Google Account security settings. Saves hours of grief.
Step 4: Update your form script
Replace the mail() call with your SMTP code. Most form builders (Contact Form 7, Gravity Forms) have SMTP plugins. For a custom form, you'll need to tweak the PHP handler.
Why This Works
SMTP sends mail through a proper mail server that your hosting provider trusts. It uses authentication so spammers can't hijack it. The PHP mail() function tries to send directly from your web server, which ISPs often block outbound port 25 or flag as spam. SMTP goes through port 587 (or 465) with a login, so it looks legitimate to the recipient's mail server.
Had a client last month whose entire print queue died because of this — their form was using mail() and their host quietly disabled it after a server update. They lost three weeks of leads before I pointed them to SMTP. Don't be that client.
Less Common Variations of the Same Issue
1. DNS and SPF Records
Even with SMTP, your emails might land in spam. Check your domain's SPF record — it should include your SMTP server. For example, if using Gmail, add include:_spf.google.com to your TXT record. Without it, recipient servers think someone is spoofing your domain.
2. SMTP Relay Blacklists
Sometimes the SMTP server itself is on a blacklist. Use a tool like MXToolbox to check if your server's IP is clean. If it's blacklisted, switch to a transactional email service like SendGrid or Mailgun — they handle deliverability better than generic SMTP.
3. Form Spam Filters Blocking Legit Mail
Some aggressive anti-spam plugins (like Akismet) silently delete form submissions that look suspicious. Check the plugin logs. You might need to whitelist certain fields or reduce sensitivity.
4. Character Encoding Issues
If your form includes special characters (é, ñ, emojis), the email might fail silently. Make sure your SMTP library uses UTF-8 encoding. In PHPMailer, add $mail->CharSet = 'UTF-8';.
Prevention: Set This Up Before You Ship
Don't wait for a client to complain. From day one:
- Never rely on
mail()in production. Use SMTP or a transactional email API. - Test with a real email address, not just your own domain. Gmail, Outlook, Yahoo all catch different issues.
- Set up logging. Most SMTP libraries log errors — capture them to a file so you can debug later.
- Use a dedicated email for the form (like
form@yourdomain.com). It keeps your personal inbox clean and makes deliverability issues easier to trace. - Monitor your SMTP server's reputation. Free tools like Google Postmaster can show you bounce rates and spam complaints.
That's it. The fix takes about 20 minutes if you've never done it before. Next time a client says "my contact form isn't working," you'll know exactly where to look.
Was this solution helpful?