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

托管Ubuntu 16.04.1 LTS中Nodemailer被拦截问题求助

Troubleshooting Your Postfix + Nodemailer Issue

Hey there! It sounds like you’ve put in a ton of work already narrowing down this problem—let’s dig into your questions and share common community experiences that might help you zero in on the root cause.

Have other users faced similar issues?

Absolutely, this is a fairly common scenario when setting up Nodemailer with a local Postfix instance. Here are the most frequent culprits other users have hit:

  • Postfix relay restrictions: Even if your local mail user exists, Postfix might be blocking relay requests from your script. Check your smtpd_relay_restrictions or smtpd_recipient_restrictions in /etc/postfix/main.cf—make sure permit_mynetworks is included and covers 127.0.0.1/32 (for localhost) and your workstation’s IP (if connecting remotely).
  • AppArmor limitations: Ubuntu’s default AppArmor security profiles can sometimes block Node.js from accessing Postfix’s SMTP port. Try temporarily stopping AppArmor (sudo systemctl stop apparmor) and running your script again—if it works, you’ll need to adjust the AppArmor rules for Node.js or Postfix.
  • Unchecked mail logs: Most users overlook the goldmine of info in /var/log/mail.log. Look for entries related to your script’s connection attempts—you might see errors like relay access denied, authentication failure, or invalid recipient that point directly to the issue.
  • SASL authentication gaps: If your Postfix instance is configured to require SASL auth (even for local connections), Nodemailer needs to pass valid credentials. A lot of users forget to include the auth object in their transporter config, leading to silent failures.

Is Nodemailer the right library for this?

100% yes. Nodemailer is the de facto standard for sending email in Node.js—it’s well-maintained, has extensive documentation, and handles every common SMTP scenario (including local Postfix setups) seamlessly. You don’t need to switch libraries here; the issue is almost certainly in configuration or environment setup, not the library itself.

A note on your code snippet

You mentioned a code fragment starting with var ...—sharing the full transporter setup would help pinpoint issues. For example, a typical local Postfix transporter config might look like this:

var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
  host: 'localhost',
  port: 25,
  secure: false, // Port 25 uses plaintext SMTP
  // Uncomment if your Postfix requires SASL auth
  // auth: {
  //   user: 'your-local-mail-user',
  //   pass: 'your-user-password'
  // }
});

// Test send function
transporter.sendMail({
  from: 'sender@your-domain.com',
  to: 'recipient@your-domain.com',
  subject: 'Test Email',
  text: 'Hello from Nodemailer!'
}, (error, info) => {
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

Double-check that your config matches your Postfix setup—if Postfix doesn’t require auth, make sure you’re not including unnecessary auth parameters, and vice versa.

Quick next steps to narrow the scope

  • Test manual SMTP connection: Use telnet localhost 25 or nc localhost 25 to send a test email manually. If this fails, the problem is with Postfix, not your code. If it works, the issue is in your Nodemailer config.
  • Verify Postfix’s mynetworks config: Run postconf mynetworks to confirm your allowed IP ranges include the machine running your script.
  • Check for Postfix service warnings: Run systemctl status postfix to ensure there are no hidden errors in the service itself.

内容的提问来源于stack exchange,提问作者otembajelle

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:27:23