Node-imap显示电子邮件

时间:2018-04-29 22:09:46

标签: imap node-imap

我一直在尝试正确显示电子邮件,但未能这样做。我只是发送电子邮件给自己,当我在hotmail enter image description here打开它时看起来像这样但是当我使用node-imap在我的应用程序中打开它时,从'发送框'看起来像这样(注意那些' ='标志,以及其他一些奇怪的东西..):

enter image description here

并且这是在控制台中输出相同的信息(出于某种原因,我在每行的末尾得到'=',并且在'='之后得到'3D'):

enter image description here

现在...... SAME消息,同样的方法,刚从收件箱中打开: http://prntscr.com/jbqtyw

在控制台中只是一个纯文本.. http://prntscr.com/jbquba

我知道它的问题很难理解,但很难理解,但不能让它变得更容易......以下是获取电子邮件的get()代码:

get(id, params) {
    this.emailUsername = params.user.businessEmail;
    this.emailPassword = params.user.businessEmailPassword;
    this.host = params.user.serverIMAP;
    this.port = params.user.portIMAP;
    this.tls = params.user.serverSMTP;
    this.smtpPort = params.user.portSMTP;

    let currBox = params.query.box;
    let userEmail = this.emailUsername;

    return new Promise((resolve, reject) => {
      var imap = new Imap({
        user: this.emailUsername,
        password: this.emailPassword,
        host: this.host,
        port: this.port,
        tls: this.tls,
        tlsOptions: {
          rejectUnauthorized: false
        }
      });
      var response = {};

      function toUpper(thing) { return thing && thing.toUpperCase ? thing.toUpperCase() : thing; }

      function findAttachmentParts(struct, attachments) {
        attachments = attachments || []
        struct.forEach((i) => {
          if (Array.isArray(i)) findAttachmentParts(i, attachments)
          else if (i.disposition && ['INLINE', 'ATTACHMENT'].indexOf(toUpper(i.disposition.type)) > -1) {
            attachments.push(i)
          }
        })
        return attachments
      }

      function checkEmail(email) {
        return email.split('@')[1].split('.')[0];
      }

      function findTextPart(struct) {
        for (var i = 0, len = struct.length, r; i < len; ++i) {
          if (Array.isArray(struct[i])) {
            if (r = findTextPart(struct[i]))
              return r;
          } else if (struct[i].type === 'text'
            && struct[i].subtype === 'html') {
              return [struct[i].partID, struct[i].type + '/' + struct[i].subtype];
            } else if(struct[i].type === 'text'&& struct[i].subtype === 'plain') {
              return [struct[i].partID, struct[i].type + '/' + struct[i].subtype];
            }
        }
      }

      function getMsgByUID(uid, cb, partID) {
        var f = imap.seq.fetch(uid,
          (partID
            ? {
              bodies: [
                'HEADER.FIELDS (TO FROM SUBJECT DATE CC BCC)',
                partID[0]
              ]
            }
            : { struct: true })),
          hadErr = false;

        if (partID)
          var msg = { header: undefined, body: '', attrs: undefined };

        f.on('error', function (err) {
          hadErr = true;
          cb(err);
        });

        if (!partID) {
          f.on('message', function (m) {
            m.on('attributes', function (attrs) {
              partID = findTextPart(attrs.struct);
              const attachments = findAttachmentParts(attrs.struct);
              attachments.forEach((attachment) => {
                const filename = attachment.params.name  // need decode disposition.params['filename*'] !!!
                const encoding = toUpper(attachment.encoding)
                const f = imap.fetch(attrs.uid, { bodies: [attachment.partID] })
              })
            });
          });
          f.on('end', function () {
            if (hadErr)
              return;
            if (partID)
              getMsgByUID(uid, cb, partID);
            else
              cb(new Error('No text part found'));
          });
        } else {
          f.on('message', function (m) {
            m.on('body', function (stream, info) {
              var b = '';
              stream.on('data', function (d) {
                b += d;
              });
              stream.on('end', function () {
                if (/^header/i.test(info.which))
                  msg.header = Imap.parseHeader(b);
                else
                  msg.body = b;
                  console.log(b);
              });
            });
            m.on('attributes', function (attrs) {
              msg.attrs = attrs;
              msg.contentType = partID[1];
            });
          });
          f.on('end', function () {
            if (hadErr)
              return;
            cb(undefined, msg);
          });
        }
      }

      imap.once('ready', function () {
        imap.openBox(currBox, true, function (err, box) {
          if (err) throw err;
          getMsgByUID(id, function (err, msg) {
            if (err) throw err;
            response = msg;
            imap.end();
          });
        });
      });


      imap.once('error', function (err) {
        reject(err);
      });

      imap.once('end', function () {
        resolve(response);
      });

      imap.connect();
    })

  }

并在前端,只显示为内部HTML:

<div class="content" [innerHtml]="bypassSecurity(email.body)">
    </div>

所以总结一下..同样的方法/调用从流中获取不同的输出(一旦是纯文本,一旦是html文本..在两种情况下都会添加一些奇怪的屎,比如'='或'3D'似乎喜欢随意的地方)?这是阅读电子邮件的正确方法吗?

1 个答案:

答案 0 :(得分:0)

这是Quoted Printable编码。

看起来您需要撤消部分的内容传输编码,这些部分通常可以是Base64或Quoted Printable。