Node.js:在Mailparser中获取'x-failed-recipients'属性

时间:2019-05-14 11:52:05

标签: node.js express nodemailer formidable email-parsing

我想使用Node.JS中的mailparser包从MBox文件中检索“ X失败的收件人”属性。

这里是MWE,我有两个文件,uploadapp.js和uploadfile.html

// File : uploadapp.js
var express = require('express');
var formidable = require('formidable');
var app = express();
var MailParser  = require('mailparser').MailParser;
var Mbox = require('node-mbox');
var mbox = new Mbox();
var fs = require('fs');

app.get('/', function (req, res){
    res.sendFile(__dirname + '/uploadfile.html');
});

app.post('/', function (req, res){
    var form = new formidable.IncomingForm();
    form.parse(req);

    form.on('fileBegin', function (name, file){
    file.path = __dirname + '/uploads/' + file.name;

    const mailbox = fs.readFileSync(file.path);
        processMailbox(mailbox);
    });

    res.sendFile(__dirname + '/uploadfile.html');
});

function processMailbox(mailbox) {
    let mbox = new Mbox(mailbox);
    // wait for message events
    mbox.on('message', function(msg) {
      // parse message using MailParser
      let mailparser = new MailParser({ streamAttachments : true });

      mailparser.on('headers', function(headers) {
        console.log('From   :', headers.get('from').value[0].address);
        console.log('Subject:', headers.get('subject'), '\n');

    // This is returning 'undefined'! How to retreive 'x-failed-receipients'?
    console.log('X-Failed-Recipients:', headers.get('x-failed-recipients'),'\n');
      });
      mailparser.write(msg);
      mailparser.end();
});
}
app.listen(3000);

然后是uploadfile.html

<!-- File : uploadfile.html-->
<!DOCTYPE html>
<html>
<head>
    <title>Simple Upload Example</title>
</head>
<body>
<form action="/" enctype="multipart/form-data" method="post">
    <input type="file" name="upload" multiple>
    <input type="submit" value="Upload">
</form>
</body>
</html>

我希望这个问题足够清楚。主要代码在uploadapp.js中。

有什么主意吗?

0 个答案:

没有答案