如何解决Nodemailer结合Gmail/SMTP的生产环境邮件发送问题?
Hey there, let's tackle your email delivery issues one by one and get them sorted out!
1. Fixing Gmail SMTP Authentication for Overseas Production Servers
The timeout-based 2FA token setup Google requires is indeed a hassle—luckily, there's a better way using App Passwords (designed specifically for apps that don't support modern 2FA prompts):
- First, make sure your Google account has 2FA enabled (this is a prerequisite for App Passwords)
- Go to your Google Account > Security > App Passwords
- Generate a new app password: select "Mail" as the app, choose "Other" as the device, and name it something like "Nodemailer Production"
- Replace your current
passvalue in the code with this generated App Password - Also, fix your transporter config for production (disable the unsafe TLS bypass and use proper SSL):
const nodemailer = require('nodemailer'); // No need for separate nodemailer-smtp-transport—Nodemailer has this built-in now const transporter = nodemailer.createTransport({ service: 'gmail', host: 'smtp.gmail.com', port: 465, secure: true, // Use SSL for production auth: { user: 'info@gmail.com', pass: 'your-generated-app-password', // Replace with your App Password }, });
Note: Google has removed the "Less secure app access" option, so App Passwords are the only supported way to use Gmail SMTP with 2FA-enabled accounts.
2. Fixing SendGrid Emails Not Reaching Gmail
If SendGrid says emails are sent but Gmail isn't receiving them, it's almost always a domain authentication issue:
- Set up SPF record: Add a TXT record to your domain's DNS:
v=spf1 include:sendgrid.net ~all - Configure DKIM: Generate a DKIM key in your SendGrid dashboard, then add the corresponding TXT record to your domain DNS
- Add DMARC record: Start with a monitoring record to avoid accidental blocks:
v=DMARC1; p=none; sp=none; rua=mailto:dmarc@yourdomain.com(you can tighten this later top=quarantineorp=rejectonce things work) - Verify your sender domain: In SendGrid, go to Sender Authentication and verify your
samesoft.networkdomain—this tells Gmail the email is legitimately from you - Check SendGrid logs: Look for "dropped" or "bounced" statuses—if Gmail is marking emails as spam, tweak your content (avoid overly salesy language like "Congratulations!" and make sure the email body is relevant to your recipients)
3. Fixing Local SMTP "Sent but Not Received" Problem
If your local SMTP server logs say emails are sent but you don't get them, try these checks:
- Relay configuration: Most local SMTP servers (like Postfix) need a relay setup to send external emails—without it, emails just sit in the local queue instead of being forwarded
- ISP blocks: Many ISPs block outbound port 25 (default SMTP port). Try using port 587 (STARTTLS) or 465 (SSL) instead
- Check server logs: Look for errors in your local SMTP logs (e.g.,
/var/log/mail.logon Linux) — common issues include "relay access denied" or "connection refused" when trying to reach Gmail's SMTP servers - Test with a tool: Use
swaksortelnetto manually test your local SMTP:
This will show you exactly where the delivery is failing.swaks --to your-gmail-address@gmail.com --from no-reply@samesoft.network --server localhost:587
Quick Code Tweaks
- You don't need to install
nodemailer-smtp-transportanymore—Nodemailer includes SMTP support out of the box - Make sure your
OWNER_EMAILvariable is correctly defined and points to a valid Gmail address - For better deliverability, match your
fromaddress to your authenticated domain (e.g., useinfo@samesoft.networkinstead ofno-reply@samesoft.networkif you've verifiedsamesoft.networkin your email service)
内容的提问来源于stack exchange,提问作者DimasikT
相关产品推荐
相关产品推荐

