With mailparser when I get a lot of HTML codes how can I convert them into DOM Elements? I want to access specific HTML elements.
This is my code:
var imaps = require('imap-simple');
const _ = require('lodash');
const simpleParser = require('mailparser').simpleParser;
const cheerio = require('cheerio');
var config = {
imap: {
user: 'xxx',
password: 'xxx',
host: 'xxx',
port: 993,
tls: true,
authTimeout: 4000
}
};
imaps.connect(config).then(function (connection) {
return connection.openBox('INBOX').then(function () {
var searchCriteria = [
'UNSEEN'
];
var fetchOptions = {
bodies: ['HEADER', 'TEXT', ''],
};
return connection.search(searchCriteria, fetchOptions).then(function (messages) {
messages.forEach(function (item) {
var all = _.find(item.parts, { "which": "" })
var id = item.attributes.uid;
var idHeader = "Imap-Id: "+id+"\r\n";
simpleParser(idHeader+all.body, (err, mail) => {
if (mail.subject.indexOf('example.com') > -1) {
var dom = mail.html;
const $ = cheerio.load(mail.html);
var testing = $('span')[0].text();
} else {
//return false;
}
});
});
});
});
});
I tried with cheerio but had no luck. How can I get access to each HTML element?
Thanks.
$('span')[0]
isn't a cheerio object. You can try $('span').first().text()
instead.