nodemailer / smtp-server

Create custom SMTP servers on the fly
Other
862 stars 146 forks source link

TypeError: Cannot read properties of undefined (reading 'toLowerCase') #195

Closed Hamza339 closed 1 year ago

Hamza339 commented 1 year ago

const server = new SMTPServer({ // The hostname for the server to use in greeting messages. banner: 'My Custom SMTP Server',

// Allow authentication authOptional: true,

// Handler for the HELO/EHLO command onHelo: (session, callback) => { console.log('HELO/EHLO:', session.hello.host); callback(); },

// Handler for the MAIL FROM command onMailFrom: (address, session, callback) => { console.log('MAIL FROM:', address.address); session.envelope.mailFrom = address.address; callback(); },

// Handler for the RCPT TO command onRcptTo: (address, session, callback) => { console.log('RCPT TO:', address.address); session.envelope.rcptTo.push(address.address); callback(); },

// Handler for the DATA command onData: (stream, session, callback) => { console.log('DATA'); let data = ''; stream.on('data', (chunk) => { data += chunk; });

stream.on('end', () => {
  // Process the email data (data contains the entire email content)
  console.log('Received email:');
  console.log(data);

  // You can do more processing here, like parsing the email content,
  // storing it in a database, or forwarding it to another SMTP server.

  // Respond to the client
  callback();
});

}, });

server.on('error', (err) => { console.log('Error occurred:', err.message); });

server.listen(25, '0.0.0.0', () => { console.log('Custom SMTP server is listening on port 25.'); });

app.post('/send', async (req, res) => { // await startSMPTServer(); const { to, subject, text } = req.body; console.log("Sending email ...", req.body); // Replace these details with your actual SMTP server configuration const smtpConfig = { host: 'localhost', // Replace with your SMTP server IP or hostname port: 25, // Replace with your SMTP server port secure: false, tls: { rejectUnauthorized: false, // Accept self-signed certificate during development/testing } };

const transporter = nodemailer.createTransport(smtpConfig);

const mailOptions = { from: 'hamza@geekgenix.com', // Replace with the sender email address to, subject, text, };

try { console.log("Sending email ...", mailOptions); // Send the email using nodemailer and your custom SMTP server await transporter.sendMail(mailOptions); res.json({ message: 'Email sent successfully.' }); } catch (error) { console.error('Failed to send email:', error.message); res.status(500).json({ error: 'Failed to send email.' }); } }); this is my code but i am getting error from your package

MAIL FROM: hamza@geekgenix.com RCPT TO: zafar@geekgenix.com C:\Users\Asus\Desktop\Oasis-Services\MailingService-Oasis\node_modules\smtp-server\lib\smtp-connection.js:1219 if (this.session.envelope.rcptTo[i].address.toLowerCase() === parsed.address.toLowerCase()) { ^

TypeError: Cannot read properties of undefined (reading 'toLowerCase') at C:\Users\Asus\Desktop\Oasis-Services\MailingService-Oasis\node_modules\smtp-server\lib\smtp-connection.js:1219:61 at SMTPServer.onRcptTo (C:\Users\Asus\Desktop\Oasis-Services\MailingService-Oasis\app.js:77:5) at SMTPConnection.handler_RCPT (C:\Users\Asus\Desktop\Oasis-Services\MailingService-Oasis\node_modules\smtp-server\lib\smtp-connection.js:1211:22) at SMTPConnection._onCommand (C:\Users\Asus\Desktop\Oasis-Services\MailingService-Oasis\node_modules\smtp-server\lib\smtp-connection.js:497:17) at SMTPStream.SMTPConnection._parser.oncommand (C:\Users\Asus\Desktop\Oasis-Services\MailingService-Oasis\node_modules\smtp-server\lib\smtp-connection.js:58:52) at readLine (C:\Users\Asus\Desktop\Oasis-Services\MailingService-Oasis\node_modules\smtp-server\lib\smtp-stream.js:128:22) at SMTPStream._write (C:\Users\Asus\Desktop\Oasis-Services\MailingService-Oasis\node_modules\smtp-server\lib\smtp-stream.js:132:13) at writeOrBuffer (node:internal/streams/writable:391:12) at _write (node:internal/streams/writable:332:10) at SMTPStream.Writable.write (node:internal/streams/writable:336:10) at TLSSocket.ondata (node:internal/streams/readable:754:22) at TLSSocket.emit (node:events:513:28) at addChunk (node:internal/streams/readable:315:12) at readableAddChunk (node:internal/streams/readable:289:9) at TLSSocket.Readable.push (node:internal/streams/readable:228:10) at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23) [nodemon] app crashed - waiting for file changes before starting...

andris9 commented 1 year ago

Do not modify session.envelope object