nodemailer / smtp-server

Create custom SMTP servers on the fly
Other
846 stars 145 forks source link

smtp-server is not sending emails #185

Closed rudimadima closed 1 year ago

rudimadima commented 1 year ago

Hi. I have installed Postfix on my VPS, I created SPF and PTR records in my domain and emails are normally sent from my server to gmail via Postfix. But I want to replace Postfix with smtp-server. But no matter what I do, emails are not sent to gmail!

My transporter:

export const transporter2 = nodemailer.createTransport({
  host: 'mail.example.pro',
  port: 587,
  // secure: true, // use TLS when connecting to server
  auth: {
    user: 'admin',
    pass: '123',
  },
  tls: {
    rejectUnauthorized: false, // solves problem with 'self signed certificate'
  },
});

Use of transporter:

    const result = await transporter2.sendMail({
      from: '"Some Name" <no-reply@example.pro>', 
      to: 'somegmailaccout@gmail.com',
      subject: 'Please, work!',
      text: `Please, work!`,
    });

    console.log(result); // { response: '250 OK: message queued', ... 

My smtp-server:

import { SMTPServer } from 'smtp-server';
import fs from 'fs';

console.log('Starting smtp-server');

const server = new SMTPServer({
  // secure: true,
  // key: fs.readFileSync('/etc/letsencrypt/live/mail.example.pro/privkey.pem'),
  // cert: fs.readFileSync('/etc/letsencrypt/live/mail.example.pro/fullchain.pem'),
  name: 'mail.example.pro',

  onAuth (auth, session, callback) {
    console.log('client IP: ', session.remoteAddress);
    if (auth.username === 'admin' && auth.password === '123') {
      callback(null, {
        user: { name: auth.username },
      });
    } else {
      callback(new Error('Wrong login/password'));
    }
  },

  onMailFrom(address, session, callback) {
    if (address.address !== 'no-reply@example.pro') {
      return callback(new Error('Bad email address'));
    }
    return callback();
  },

  onData(stream, session, callback) {
    stream.pipe(process.stdout); // print message to console
    stream.on('end', callback);
  },
});

server.listen(587);

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

It accepts nodemailer transporter, but doesn't make deliveries to gmail. What can I do to fix it?

andris9 commented 1 year ago

smtp-server is a library to create SMTP interfaces for other apps. It does not deliver anything itself, it accepts messages and passes these to you app for further processing.

krm35 commented 1 year ago

EDIT : you can use https://github.com/guileen/node-sendmail to send email, don't forget to setup dns records. Btw smtp-server is perfect to receive emails and store them in a database.

const sendmail = require('sendmail')({
    logger: {
        debug: console.log,
        info: console.info,
        warn: console.warn,
        error: console.error
    },
    silent: false
});

sendmail({
    from: 'no-reply@example.pro',
    to: 'somegmailaccout@gmail.com',
    subject: 'Please, work!',
    html: 'It worked',
}, function(err, reply) {
    console.log(err && err.stack);
    console.dir(reply);
});

I was also looking for a way to send emails and just found out it's not possible with smtp-server.

https://nodemailer.com/extras/smtp-server/

This is not a full-blown server application like Haraka but an easy way to add custom SMTP listeners to your app. This module does not make any email deliveries by itself. smtp-server allows you to listen on ports 25/24/465/587 etc. using SMTP or LMTP protocol and that's it. Your own application is responsible of accepting and delivering the message to destination.