NodeMailer报错:Cannot read property 'getSocket' of undefined 求助
Hey there, let's work through this error you're hitting. That Cannot read property 'getSocket' of undefined usually pops up when Nodemailer's transport instance isn't properly initialized, or there's a misstep in how you're passing data around. Let's break down the fixes step by step:
1. Fix the Obvious Typo in Your Code
First, looking at your send function snippet:
const html = generateHTML(options.filename, option...
You've got a typo here—option... should be options (or properly destructured if you only need parts of it). This could lead to invalid data being passed to your template renderer, which might cascade into issues with the transport later. Correct it like this:
// Either pass the full options object const html = generateHTML(options.filename, options); // Or destructure if you want to split out the filename const { filename, ...emailDetails } = options; const html = generateHTML(filename, emailDetails);
2. Verify Your Environment Variables Are Loaded
Your transport relies on process.env values, but if these aren't properly loaded (e.g., you forgot to use dotenv or your env file is misconfigured), createTransport will get undefined for critical fields like host or port. This creates a broken transport instance that throws the getSocket error when you try to send mail.
Add a quick log before creating the transport to confirm values exist:
console.log('Mail Configuration:', { host: process.env.MAIL_HOST, port: process.env.MAIL_PORT, user: process.env.MAIL_USER }); // Ensure none of these are undefined or empty strings! const transport = nodemailer.createTransport({ /* ... */ });
3. Fix Transport Configuration Issues
Nodemailer v4.6.4 needs specific configuration depending on your mail provider. Here are two common fixes:
- Convert port to a number:
process.envreturns strings, but Nodemailer expects a numeric port. - Add security settings: Match
secureto your port (465 usessecure: true, 587 usessecure: false) and add TLS settings if needed for testing.
Update your transport setup:
const transport = nodemailer.createTransport({ host: process.env.MAIL_HOST, port: parseInt(process.env.MAIL_PORT, 10), // Convert to number secure: process.env.MAIL_PORT === '465', // Auto-set based on port auth: { user: process.env.MAIL_USER, pass: process.env.MAIL_PASS }, tls: { // Disable unauthorized rejection for non-production environments rejectUnauthorized: process.env.NODE_ENV !== 'production' } });
4. Validate the Transport Instance Before Sending
Add a quick check in your send function to ensure the transport is valid before proceeding:
exports.send = async (options) => { if (!transport || !transport.sendMail) { throw new Error('Email transport failed to initialize correctly'); } // Corrected parameter handling const { filename, to, subject } = options; const html = generateHTML(filename, options); const sendResult = await transport.sendMail({ from: process.env.MAIL_FROM, // Don't forget this required field! to, subject, html }); return sendResult; };
Bonus: Check Node.js Version Compatibility
While Node.js v8.6.0 should work with Nodemailer v4.6.4, it's an older version. If the above fixes don't resolve the issue, consider upgrading Node.js to a newer LTS release (like v10.x or v12.x) to rule out any hidden compatibility bugs.
内容的提问来源于stack exchange,提问作者aazcast

