I created a local smtp server using localhost and port 25, I want to be able to create a custom local smtp server and send email using nodemailer. Currently I have this code:
const SMTPServer = require('smtp-server').SMTPServer;
const server = new SMTPServer({
authOptional: true,
onData(stream, session, callback) {
let message = '';
stream.on('data', (chunk) => {
message += chunk.toString();
});
stream.on('end', () => {
console.log('Received message:');
console.log(message);
callback();
});
},
onClose(session) {
console.log('Connection closed');
},
});
server.on('error', (err) => {
console.error('SMTP Server Error:', err);
});
server.listen(25, '0.0.0.0', () => {
console.log('SMTP Server listening on port 25');
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
host: 'localhost', // Replace with your SMTP server address
port: 25, // Use the appropriate port
secure: false,
tls : { rejectUnauthorized: false }
});
const mailOptions = {
from: 'myTesteEmail@localhost',
to: '[email protected]',
subject: 'Test Email',
text: 'Hello, this is a test email.',
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error('Error sending email:', error);
} else {
console.log('Email sent:', info.response);
}
});
});
I am getting this message everyTime I try to run de code:
SMTP Server listening on port 25
Received message:
Content-Type: text/plain; charset=utf-8
From: myTesteEmail@localhost
To: [email protected]
Subject: Test Email
Message-ID: da0329f7-8b6f-9005-e154-eeff9c62c61e@localhost
Content-Transfer-Encoding: 7bit
Date: Sat, 23 Dec 2023 01:45:02 +0000
MIME-Version: 1.0Hello, this is a test email.
Email sent: 250 OK: message queued
Connection closed
But when I check my email, I got no email from my local smtp. I even tried putting the from email to the same as To email with my real gmail account, but still not working, how can I make this working?
I searched a lot, but I coudln’t find any tutorial using a custom local smtp server, they was using an already existing smtp services like mailtril, bravo..
I tried Chat gpt, but no working