Heroku平台PHP发送邮件失败求助:新手需分步配置指导
Hey there! I totally get how frustrating it is when you’re new to Heroku and your email code just won’t work—especially when the PHP itself checks out. Let’s walk through fixing this step by step, since I’ve helped tons of folks with this exact issue.
First, let’s spot a quick config mistake in your existing code that’s probably causing problems:
Step 1: Fix the SMTP Port/Encryption Mismatch
Your code has a common mismatch between encryption type and port:
TLSencryption uses port 587SSLencryption uses port 465
Right now you’ve set $mail->SMTPSecure = 'tls'; but $mail->Port = 465;—that’s going to break the connection. Pick one pair and update your code:
// Option 1: TLS + 587 (recommended) $mail->SMTPSecure = 'tls'; $mail->Port = 587; // OR Option 2: SSL + 465 $mail->SMTPSecure = 'ssl'; $mail->Port = 465;
Step 2: Fix Gmail’s Security Restrictions (If You’re Sticking with Gmail)
Gmail no longer allows "less secure apps" to access your account, so if you’re using a personal Gmail address:
- Enable two-factor authentication on your Google account (this is required to create an app password)
- Go to your Google Account security settings, find the "App Passwords" section (only visible after 2FA is on)
- Create a new app password for "Mail" and name it something like "Heroku PHP App"
- Replace your hardcoded
mypasswordin the code with this 16-character app password
Step 3: Stop Hardcoding Credentials (Use Heroku Environment Variables)
Hardcoding your email username and password is unsafe and makes it hard to update later. Let’s move these to Heroku’s environment variables:
- Open your terminal and run these commands (replace with your actual details):
heroku config:set GMAIL_USERNAME=your-email@gmail.com heroku config:set GMAIL_APP_PASSWORD=your-generated-app-password
- Update your PHP code to pull these values from the environment:
$mail->Username = getenv('GMAIL_USERNAME'); $mail->Password = getenv('GMAIL_APP_PASSWORD');
Step 4: Enable Debugging to Diagnose Further Issues
If it’s still not working, turn on PHPMailer’s debug mode to see exactly what’s failing:
$mail->SMTPDebug = 2; // This will show detailed connection logs
Then deploy your code and check Heroku’s logs in real-time:
heroku logs --tail
The logs will tell you if there’s an authentication failure, connection timeout, or other specific error to fix.
Step 5: Switch to a Heroku Email Add-On (More Reliable Than Gmail)
Gmail has strict sending limits and often marks app emails as spam. For a more reliable solution, use one of Heroku’s official email add-ons like SendGrid or Mailgun. Here’s how to set up SendGrid (free tier available):
- Install the SendGrid add-on:
heroku addons:create sendgrid:starter
- Heroku will automatically set a
SENDGRID_API_KEYenvironment variable for you - Update your PHPMailer code to use SendGrid’s SMTP server:
$mail->Host = 'smtp.sendgrid.net'; $mail->SMTPAuth = true; $mail->Username = 'apikey'; // This is fixed, don't change it $mail->Password = getenv('SENDGRID_API_KEY'); $mail->SMTPSecure = 'tls'; $mail->Port = 587;
This setup is designed for app-specific email sending and will avoid most of Gmail’s restrictions.
Give these steps a try—start with fixing the port/encryption mismatch first, since that’s the most common quick fix. Let me know if you hit any snags with the logs or add-on setup!
内容的提问来源于stack exchange,提问作者Aqilhex

