8
I’m using the library of imap to log into a company account and bring in the new emails, when it’s an email reply I’d like to pick up only the new content but I can’t get it just the full conversation. Let me give you an example:
A client sends me an e-mail with the following content: 'Hi'
I reply by saying: 'Hi all right ?'
He says to me: 'Everything great'
When I go check my email box by Node to save the text. I just want to pick up the last piece of content he sent me that would be 'All great', but the imap
returns me the whole conversation like this:
Lorem ipsum dolor sit Amet, consectetur adipiscing Elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 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."
5 de April de 2018 10:50, [email protected] (mailto:[email protected]) escreveu:teste
April 5, 2018 10:31, [email protected] (mailto:[email protected]) wrote: client response
5 de April de 2018 10:30, [email protected] (mailto:[email protected]) escreveu:resposta atendimento
5 April 2018 09:45, [email protected] (mailto:[email protected]) wrote: email sent by customer
In this example of the log I would like to pick up only lorem ipsum... which is the content of the last reply email.
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 would be very grateful. I thank you.
The email client will create a conversation, with what you send and what you receive, ok. You want to pick up only the last received reply or the last message, even if it is yours, that you sent?
– Sam
The last message the client sent me, that is the last received reply
– Erick Zanetti
Can you show us what a conversation would look like in the Gmail pattern? You can create a Jsfiddle only with the text of the conversation (of course, by replacing the original texts with lorem ipsum or any example text)
– Sam
link to text document .I put in this document an example of how
console.log(mail.text)
of a Gmail conversation. The content is irrelevant is just a copy of a promotional text I received, and used only for testing, the important thing would be to take this content.– Erick Zanetti
Dude, it’s complicated to do that because the patterns can be different from one email client to another. The interesting thing would be to check the patterns of the mail clients that you work with and adjust the code that I posted so that it covers everyone as much as possible. I see this is the only way to resolve the issue. If you want I can create a chat here so that we can work on it, so as to arrive at a code as close to 100%, if the code of my answer can no longer be.
– Sam