托管Ubuntu 16.04.1 LTS中Nodemailer被拦截问题求助
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_restrictionsorsmtpd_recipient_restrictionsin/etc/postfix/main.cf—make surepermit_mynetworksis included and covers127.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 likerelay access denied,authentication failure, orinvalid recipientthat 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
authobject 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 25ornc localhost 25to 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
mynetworksconfig: Runpostconf mynetworksto confirm your allowed IP ranges include the machine running your script. - Check for Postfix service warnings: Run
systemctl status postfixto ensure there are no hidden errors in the service itself.
内容的提问来源于stack exchange,提问作者otembajelle

