How to retrieve "sent" mailbox in imap-simple
Asked Answered
T

2

5

I use imap-simple in node js. I want retrieve sent mailbox of gmail. My code is like below:

 getSent(searchCriteria: string[], callBack: any) {
        imaps.connect(this.imapConfig).then(function (connection) {

            return connection.openBox('SENT').then(function () {
                var fetchOptions = {
                    bodies: ['HEADER', 'TEXT', ''],
                    markSeen: false
                };

                return connection.search(searchCriteria, fetchOptions).then(function (results) {

                    let mails = results.map(res => {
                        return {
                            part: res.parts.find(part => part.which === ''),
                            attributes: res.attributes
                        };
                    });

                    mails = [].concat(...mails);

                    mails = mails.map(mail => {
                        return new Promise((resolve, reject) => {

                            var id = mail.attributes.uid;
                            var idHeader = "Imap-Id: " + id + "\r\n";

                            simpleParser(idHeader + mail.part.body, (error, mail) => {
                                if (error)
                                    reject(error);
                                else {
                                    resolve(new Email({
                                        sentDate: mail.date,
                                        from: mail.from.text,
                                        to: mail.to.text,
                                        messageId: mail.messageId,
                                        body: mail.html,
                                        subject: mail.subject
                                    }));
                                }
                            });
                        })
                    });

                    Promise.all(mails).then(response => {
                        callBack({
                            success: true,
                            emails: response,
                            error: undefined
                        });
                    }, error => {
                        callBack({
                            success: false,
                            emails: [],
                            error: error
                        });
                    });

                }, function (error) {
                    callBack({
                        success: false,
                        emails: [],
                        error: error
                    });
                });
            }, function (error) {
                callBack({
                    success: false,
                    emails: [],
                    error: error
                });
            });
        }, function (error) {
            callBack({
                success: false,
                emails: [],
                error: error
            });
        });
    }

if I call getSent method as below

  this.getSent(['ALL'], response => {
    
  });
 

I get the below error 'Error: Unknown Mailbox: SENT (Failure)'

I try connection.openBox('[Gmail]/Sent Mail'), but i get the similar error as

'Error: Unknown Mailbox: [Gmail]/Sent Mail (Failure)'

I use gmail.

Thrombophlebitis answered 27/1, 2020 at 11:37 Comment(8)
I could be wrong, but I'm vaguely remembering that the name of the sent folder in Gmail is "Sent Email" rather than "Sent Mail"/"Sent", have you tried that?Faxun
i already try "Sent Mail" and "Sent", but i get the similar errorThrombophlebitis
Does this answer your question? Get Gmail All folder IMAP PHPStiles
FYI, this answer is a partial duplicate. Your problem is the same, except that you want \sent, not \all as mailbox flag, and that you want it in node.js rather than php (excuse my french). node-imap supports mailbox flags, imap-simple does not, IIRC.Stiles
If you solved your own question - feel free to post the answer yourself!Hydrocephalus
dear @Stiles My google account was working at Azerbaijan language, therefore "sent box" ' s name translated Azerbaijan language. If google accout working at Azerbaijan language, box name will be [Gmail]/Göndərilənlər. Or change google's language to english, then sentbox' name will be [Gmail]/Sent Mail with google's language.Thrombophlebitis
thanks you @Hydrocephalus . I already posted answer, but i can't accept own answer for now , i will accept that tomorowThrombophlebitis
Oh, I assumed you wanted a general answer, not one that works only for one account / one language.Stiles
T
6

I got all the folder names using the getBoxes method. Then I saw that folder names come in Azerbaijan language.

connection.getBoxes().then(response => {
                var r = response;
            });

The problem was in google settings. I used Google in Azerbaijan language. If use google different language, folder names are automatically translated into that language. I change Google language to English, problem solved. It way work me. Thanks to everyone.

enter image description here

I use [Gmail]/Sent Mail.

Correct version of my code is below:

getSent(searchCriteria: string[], callBack: any) {
        imaps.connect(this.imapConfig).then(function (connection) {

            return connection.openBox('[Gmail]/Sent Mail').then(function () {
                var fetchOptions = {
                    bodies: ['HEADER', 'TEXT', ''],
                    markSeen: false
                };

                return connection.search(searchCriteria, fetchOptions).then(function (results) {

                    let mails = results.map(res => {
                        return {
                            part: res.parts.find(part => part.which === ''),
                            attributes: res.attributes
                        };
                    });

                    mails = [].concat(...mails);

                    mails = mails.map(mail => {
                        return new Promise((resolve, reject) => {

                            var id = mail.attributes.uid;
                            var idHeader = "Imap-Id: " + id + "\r\n";

                            simpleParser(idHeader + mail.part.body, (error, mail) => {
                                if (error)
                                    reject(error);
                                else {
                                    resolve(new Email({
                                        sentDate: mail.date,
                                        from: mail.from.text,
                                        to: mail.to.text,
                                        messageId: mail.messageId,
                                        body: mail.html,
                                        subject: mail.subject
                                    }));
                                }
                            });
                        })
                    });

                    Promise.all(mails).then(response => {
                        callBack({
                            success: true,
                            emails: response,
                            error: undefined
                        });
                    }, error => {
                        callBack({
                            success: false,
                            emails: [],
                            error: error
                        });
                    });

                }, function (error) {
                    callBack({
                        success: false,
                        emails: [],
                        error: error
                    });
                });
            }, function (error) {
                callBack({
                    success: false,
                    emails: [],
                    error: error
                });
            });
        }, function (error) {
            callBack({
                success: false,
                emails: [],
                error: error
            });
        });
    }
Thrombophlebitis answered 28/1, 2020 at 6:43 Comment(0)
G
1

I had the same issue and I had to figure it out myself:

Replace connection.openBox('SENT') with connection.openBox('INBOX.Sent') OR connection.openBox('INBOX/Sent').

Depending on your assigned delimiter (. or / or any other) and you can find out your delimiter by adding

connection.getBoxes().then(response => {
  var r = response;
  console.log(r); // if nothing then try console.log(JSON.stringify(r))
});

I hope this will resolve your issue. Have a nice day!

Glossitis answered 14/11, 2022 at 20:34 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.