I'm using the imap library to sign in to a company account and bring in the new emails when it's an answer of an email I would just pick up the new content but I can not get this just the full conversation. Let me give you an example:
A client sends me an email with the following content: 'Hi'
I respond by saying 'Hi, how are you?'
He answers me: 'Everything is great'
When I check my mailbox for the node to save the text. I want to get only the last content that he sent me that would be 'All right', but the imap
library returns the whole conversation to me like this:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed of eiusmod tempor inciidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, it wanted nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo conseat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. "
April 5, 2018 10:50, [email protected] (mailto: [email protected]) wrote: test
April 5, 2018 10:31, [email protected] (mailto: [email protected]) wrote: customer response
April 5, 2018 10:30, [email protected] (mailto: [email protected]) wrote: answer answer
April 5, 2018 09:45, [email protected] (mailto: [email protected]) wrote: email sent by the client
In this example of the log I would like to take only the lorem ipsum ... which is the content of the last email reply.
My code:
imap.once('ready', function () {
openInbox(imap, function (err, box) {
if (err) throw err;
var f = imap.seq.fetch('1:9999', {
bodies: [''],
struct: true
});
f.on('message', function (msg, seqno) {
msg.on('body', function (stream, info) {
var buffer = '';
var prefix = '(#' + seqno + ') ';
stream.on('data', function (chunk) {
buffer += chunk.toString('utf8');
});
stream.once('end', function () {
simpleParser(buffer).then(function(mail){
var email = {};
if (mail.headers.has('date')) {
email.date = (mail.headers.get('date'));
}
if (mail.headers.has('subject')) {
email.subject = (mail.headers.get('subject'));
}
if (mail.headers.has('from')) {
email.address = (mail.headers.get('from').value[0].address);
}
if (mail.inReplyTo) {
console.log(mail.text);
console.log('----');
} else {
email.text = mail.text;
}
// console.log(email);
}).catch(err => {
console.log(err);
});
});
});
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!');
return imap.end();
});
});
});
imap.once('error', function (err) {
console.log(err);
});
imap.once('end', function () {
console.log('Connection ended');
});
imap.connect();
If someone could give me some help I will be very grateful. Thanks in advance.