1

我正在使用 npm 包(node-imap)来检索电子邮件的正文,并且我能够成功地检索到电子邮件的正文,但是它=在最后一行的每一行之后都转储了

我的代码是:

var Imap = require('imap'),
    inspect = require('util').inspect;

var imap = new Imap({
  user: 'XXXXXXXX',
  password: 'XXXXXX',
  host: 'imap.gmail.com',
  port: 993,
  tls: true
});

function openInbox(cb) {
  imap.openBox("INBOX", true, cb);
}

imap.once('ready', function() {
    openInbox(function(err, box) {
      if (err) throw err;
      var f = imap.seq.fetch(box.messages.total + ':*', { bodies: ['HEADER.FIELDS (FROM)','TEXT'] });
      f.on('message', function(msg, seqno) {
        console.log('Message #%d', seqno);
        var prefix = '(#' + seqno + ') ';
        msg.on('body', function(stream, info) {
          if (info.which === 'TEXT')
            console.log(prefix + '\nBody [%s] found, %d total bytes\n', inspect(info.which), info.size);
          var buffer = '', count = 0;
          stream.on('data', function(chunk) {
            count += chunk.length;
            buffer += chunk.toString('utf8');
            if (info.which === 'TEXT')
              console.log(prefix + 'Body [%s] (%d/%d)', inspect(info.which), count, info.size);
          });
          stream.once('end', function() {
            if (info.which !== 'TEXT')
              console.log(prefix + 'Parsed header: %s', inspect(Imap.parseHeader(buffer)));
            else
              console.log(prefix + 'Body [%s] Finished', inspect(info.which));
            console.log(buffer.toString());
          });
        });
        msg.once('attributes', function(attrs) {
          console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8));
        });
        msg.once('end', function() {
          console.log(prefix + 'Finished');
        });
      });
      f.once('error', function(err) {
        console.log('Fetch error: ' + err);
      });
      f.once('end', function() {
        console.log('Done fetching all messages!');
        imap.end();
      });
    });
});

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

imap.once('end', function() {
  console.log('Connection ended');
});

imap.connect();

我得到的输出是这样的:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.=
w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns=3D"http://www.w3.=
org/1999/xhtml">        <head>          <title>Untitled Page</title>    </head> <body>          =
<table style=3D"text-align:left;font-family:Tahoma;font-size:small;" =
width=3D"100%">                 <tr>                            <td style=3D"font-weight:bold; =
text-align:center;">                            &nbsp;                          </td>               </tr>
        <tr>                            <td =
style=3D"font-weight:bold; text-align:left;">                                   Dear User,                      </td>
                =
</tr>                   <tr>                            <td align=3D"center" style=3D"text-align: left">
                        =
<br/>Request is generated for reset password.                           </td>                   </tr><tr>       <td =
align=3D"center" style=3D"text-align: left">            <br/>Please click here to =
reset password. </td></tr><tr>  <td style=3D"font-weight:bold; =
text-align:left;" >             <br/>           <a style=3D" background-color:#4d90fe;border:1p=
x solid #3079ed;border-radius:2px;color:white;                  display:inline-block;font-=
family:Roboto,Arial,Helvetica,sans-serif;font-size:11px;                        =
font-weight:bold;min-height:29px;line-height:29px;min-width:54px;outline:0p=
x;padding:0 8px;                        padding:0 8px;text-align:center;text-decoration:none;cur=
4

1 回答 1

1

试试这个:

通过安装quoted-printable,您可以解决此问题。

npm i quoted-printable

更多详情:https ://www.npmjs.com/package/quoted-printable

谢谢!!!

于 2019-02-01T08:06:38.197 回答