Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

email - Node.js: smtp-server & nodemailer don't send messages to remote hosts, but save them to localhost

There are a server writing with smtp-server and sender with nodemailer. The letters from remote addresses are accepted, but if I try to send a message from my server to remote address, it does not reach, but is received as if from outside. What is the reason?

Here is a code of server and sender (some logins & passwords is replaced):

server.js

"use strict";

const DOMAIN = "sode.su";

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

const server = new SMTPServer({
    secure: false,
    key:    fs.readFileSync("/etc/letsencrypt/live/sode.su/privkey.pem"),
    cert:   fs.readFileSync("/etc/letsencrypt/live/sode.su/cert.pem"),

    onRcptTo({address}, session, callback)
    {
        if (address.startsWith("noreply@"))
            return callback(new Error(`Address ${address} is not allowed receiver`));
        callback();
    },

    onAuth(auth, session, callback)
    {
        if (auth.username !== "admin" || auth.password !== "FFFFFFFF")
            return callback(new Error("Invalid username or password!"));
        callback(null, { user: 1 });
    },

    onConnect(session, callback) {
        return callback();
    },

    onClose(session) {},

    onMailFrom(address, session, callback) {
        return callback();
    },

    authOptional: true,

    onData(stream, session, callback)
    {
        var parts = [];
        var buffer;
    
        stream.on("data", data => {
            parts.push(data);
        });

        stream.on("end", () => {
            buffer = Buffer.concat(parts);

            // There the buffer contains my message that I try to send
        });
    }
});




process.on('uncaughtException', err => {
    console.error("Caught exception: " + err);
});

server.listen(25);

sender.js

const nodemailer = require("nodemailer");

(async () => {
    try {
        var transporter = nodemailer.createTransport({
            host: "smtp.sode.su",
            port: 25,
            secure: false,
            auth: {
                user: "admin",
                pass: "FFFFFFFFFFFFF"
            },
            tls: {
                rejectUnauthorized: false
            },
        });

        var mess = {
            from:    "Arelive <[email protected]>",
            to:      "[email protected]",
            subject: "Hello",
            text:    "Hello!"
        };

        var info = await transporter.sendMail(mess);
        console.log("Message sent:
", info);

    } catch (err) {
        console.error(err);
    }
}) ();

I really don't have idea where is the bug.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...