You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

  • TLS encryption uses port 587
  • SSL encryption 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:

  1. Enable two-factor authentication on your Google account (this is required to create an app password)
  2. Go to your Google Account security settings, find the "App Passwords" section (only visible after 2FA is on)
  3. Create a new app password for "Mail" and name it something like "Heroku PHP App"
  4. Replace your hardcoded mypassword in 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:

  1. 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
  1. 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):

  1. Install the SendGrid add-on:
heroku addons:create sendgrid:starter
  1. Heroku will automatically set a SENDGRID_API_KEY environment variable for you
  2. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:51:06