Could not send mail to users email using my smtp server on website

288 Views Asked by At

My goal is sending mail from smtp server on my website (lets say: example.com) to user (lets say: [email protected]).

What I have done

I created SMTP server on my website using nodejs module "smtp-server" and tested it by sending mail from [email protected] to [email protected] successfully by referring to website: https://simonjcarr.medium.com/create-an-smtp-server-with-nodejs-5688d8fd882e. code for my server is attached below

const SMTPServer = require("smtp-server").SMTPServer;
const parser = require("mailparser").simpleParser

const server = new SMTPServer({
    onData(stream, session, callback) {
        parser(stream, {}, (err, parsed) => {
          if (err){
            console.log("Error:" , err);
            return callback(new Error("Error occured"));
          }
          
          console.log(parsed)
          stream.on("end", callback)
          return callback(); // Accept the connection
        })
        
      },
      disabledCommands: ['AUTH'],
});

server.listen(25 ,() => {
    console.log("Email Server is on.")
});

What I tried

I tried sending mail from my laptop to "[email protected]" using code written using "nodemailer" for which I referred https://nodemailer.com/about/. I assumed that if I mention gmail account in "to" option and my server in "host" and run command from my laptop then my mail will be sent to [email protected] via my SMTP server on example.com. The code I used for sending mail is as below:

var nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
    port: 25, // Postfix uses port 25
    host: 'mail.example.com',
    tls: {
      rejectUnauthorized: false
    },
    secure: false,
  });
  
  var message = {
    from: '[email protected]',
    to: '[email protected]',
    subject: 'Confirm Email',
    text: 'Please confirm your email',
    html: '<p>Please confirm your email</p>'
  };
  
  transporter.sendMail(message, (error, info) => {
    if (error) {
        return console.log(error);
    }
    console.log('Message sent: %s', info.messageId);
  });

Result

However, when I run this code I get response on my SMTP server but mail never reaches gmail account. Kindly help me making it work.

0

There are 0 best solutions below