javascript - NodeJS 电子邮件别名服务器

标签 javascript node.js email alias forwarding

场景很简单:

  • 我拥有多个域名
  • 我想在这些域上运行电子邮件
  • 我希望他们无论身在何处,都可以送货到同一个地址 发送到哪个域或在哪个域上
  • 我希望他们保留原始 header 特别是 to 字段

对于这个项目,我需要使用 NodeJS

我尝试了几件事都无济于事,我非常精通 Node ,但不知道在电子邮件方面我在做什么。

我将所有域都指向了正确的位置,包括 MX 记录,并且使用 simplesmtp,我能够收到电子邮件,我只是想不出如何在不破坏标题或转发它们的情况下发送它们在这种情况下,他们会在目的地显示为两封电子邮件。

非常感谢任何建议或指导

问候, 大卫

因为我们都喜欢代码,所以这里是相关的东西

    var simplesmtp  = require("simplesmtp"),
        MailParser  = require("mailparser").MailParser,
        mailparser  = new MailParser(),
        nodemailer  = require("nodemailer"),
        gmail       = nodemailer.createTransport("SMTP",{
            service: "Gmail",
            auth: {
                user: "***",
                pass: "***"
            }
        }),
        fs          = require("fs"),
        smtp        = simplesmtp.createServer();

    // Setup the listener
    smtp.listen(25);

    // runs when the email is initially recieved
    smtp.on("startData", function(connection){
        // log out some basic stuff
        console.log("Message from:", connection.from);
        console.log("Message to:", connection.to);
        // start the write stream
        connection.saveStream = fs.createWriteStream("/path/message.txt");
    });

    // each chunk of data that is received 
    smtp.on("data", function(connection, chunk){
        // continue the write stream
        connection.saveStream.write(chunk);
    });

    // email completly received
    smtp.on("dataReady", function(connection, callback){
        // end the write stream
        connection.saveStream.end();
        // log some more stuff
        console.log("Incoming message saved to /path/message.txt");

        // start a readstream to forward out the message
        fs.createReadStream("/path/message.txt").pipe(mailparser);

        // 
        callback(null, "ABC1"); // ABC1 is the queue id to be advertised to the client
        // callback(new Error("Rejected as spam!")); // reported back to the client
    });

    // Parse the email
    mailparser.on("end", function(email){
        // now lets forward the mail
        // for now everything goes back to *****, 
        // eventually ill be setting up some configs to handle other addresses
    //  console.log(email.headers); */
    //  email.headers['X-Forwarded-To'] = "*****"; 
    //  email.to = "*****";
        delete email.headers.received;
        delete email.text;
    //  delete email.headers.X-Received; 
        email.to = email.to + ', ' + "*****";
        email.headers.to = email.to + ', ' + "*****";
        console.log(email.headers);

        gmail.sendMail(email, function(err, response){
            if(err)
                console.log(err);

            // now clean up that message file
            fs.rename('/path/message.txt', 'emails/'+new Date().toJSON()+email.subject+'.eml', function(err){
                if(err) console.log(err);
                fs.unlink('/path/message.txt', function(){console.log('clenaed up');});

            })

            // final logging
            console.log('sent');
        });
    });

最佳答案

您可以设置 envelope sender对于电子邮件 using nodemailer以便它包含转发地址:

        email.envelope = {
            from: email.from
            to: "user@example.com"
        }

某些 SMTP 服务不允许您设置信封发件人 (MAIL FROM)。您可能在使用 Gmail 时遇到问题。

另见:

关于javascript - NodeJS 电子邮件别名服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20419285/

相关文章:

php - 来自 GoDaddy 的电子邮件在 Gmail 中显示为 'via'

python - 创建 Python 电子邮件(接收)服务器

javascript - 更好地理解 Promise.prototype.then

javascript - History API 和 History.js 后退按钮问题

node.js - 如何通过 Yarn 使用本地路径安装软件包?找不到包裹

python - 如何在 Django 中更改发件人电子邮件名称

javascript - 在非输入元素中捕获 "Selstart"、 "Sel..end"

javascript - 遍历 Canvas 像素并找到每个白色像素的 x、y 和位置

node.js - 在成功构建时增加 NPM/Grunt 版本 -- Bamboo

node.js - 如何避免多个 Mongoose 操作的嵌套 promise ?