Ad

How To Fetch Email Thread By MessageId Using IMAP In NodeJS?

- 1 answer

I want to fetch whole reply thread using the particular messageId something like this [email protected].

I have tried this:

    const getInbox = () => {
    return new Promise((resolve, reject) => {
        imaps.connect(config).then(function (connection) {
            return connection.openBox('INBOX').then(function () {
                var searchCriteria = ['ALL'];
                var fetchOptions = {
                    bodies: ['HEADER', 'TEXT', ''],
                };
                return connection.search(searchCriteria, fetchOptions).then(async function (messages) {
                    let promises = messages.map(item=>{
                        return new Promise((resolve,reject)=>{
                            var all = _.find(item.parts, { "which": "" })
                            var id = item.attributes.uid;
                            var idHeader = "Imap-Id: " + id + "\r\n";
                            simpleParser(idHeader + all.body, (err, mail) => {
                                resolve(mail);
                            });
                        });
                    });
                    Promise.all(promises).then(data=>{
                        let d = data.filter(obj=>obj.me).map(obj=>{
                            return {
                                from:obj.from.value.map(obj=>obj.address).join(','),
                                to:obj.to.value.map(obj=>obj.address).join(','),
                                subject:obj.subject,
                                attachments:obj.attachments,
                                message_id:obj.messageId,
                                date:obj.date

                            }
                        });
                        resolve(d);
                    })
                });
            });
        });
    }); }

I have tried this , it is returning whole inbox, I want particular message thread.

Ad

Answer

I tried this

var searchCriteria = [['HEADER','IN-REPLY-TO',messageId]];

it is working perfectly.

Ad
source: stackoverflow.com
Ad