1

My goal is sending mail from smtp server on my website (lets say: example.com) to user (lets say: someuser@gmail.com).

What I have done

I created SMTP server on my website using nodejs module "smtp-server" and tested it by sending mail from someuser@gmail.com to noreply@example.com 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 "someuser@gmail.com" 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 someuser@gmail.com 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: 'noreply@example.com',
    to: 'someuser@gmail.com',
    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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    It's nearly impossible anymore to have your own SMTP server. The vast majority of servers will reject email from an SMTP server that they don't know about. Things like SPF, DKIM, and DMARC will help as you now have proof that your domain can send from a particular IP/domain. But, in general, you will mostly be disappointed with how hard this will end up being. You'll be better off to send through Gmail or another provider. – stdunbar Aug 04 '22 at 14:49
  • @stdunbar I am owner of my domain name and can change DNS settings so, I think that it would not be issue for me. Still please tell me if there is something I am missing. – anon999_void Aug 04 '22 at 16:44
  • Do you have SPF, DKIM, and DMARC set up? That would be the first place I'd start. The code is secondary until you have that. Google likely won't even put the message in spam. – stdunbar Aug 04 '22 at 16:48
  • @stdunbar not yet, I started setting it up after you mentioned them. – anon999_void Aug 04 '22 at 16:51
  • @stdunbar i setup SPF, DKIM properly and it does not look issue is due to that as I even tried to send mail to ethereal email instead for gmail for testing which is not that restrictive but still my mail does not reach – anon999_void Aug 12 '22 at 21:11
  • Do you get any sort of response back from Gmail? Normally you want a 250 back but SMTP has [quite a few codes](https://www.greenend.org.uk/rjk/tech/smtpreplies.html) depending on what you've sent. The 250 doesn't guarantee delivery but it's a good start. I'm not sure if that would show up in your error or not. – stdunbar Aug 12 '22 at 21:25
  • @stdunbar i am not getting any error. when I run nodemailer code for sending mail I get response: `Message sent: <458b90cc-2d6a-a883-faec-4d79b1487040@example.com>` – anon999_void Aug 12 '22 at 21:41

0 Answers0