场景很简单:
对于这个项目,我需要使用NodeJS
我已经尝试了几件事无济于事,我对节点非常熟练,但在电子邮件方面我不知道我在做什么。
我有所有域指向正确的位置,包括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');
});
});
答案 0 :(得分:2)
您可以为电子邮件envelope sender设置using nodemailer,使其包含转发地址:
email.envelope = { from: email.from to: "user@example.com" }
某些SMTP服务不允许您设置信封发件人(MAIL FROM
)。您可能无法使用Gmail。
另见: