Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read the mail body #130

Closed
aymenbouhaha opened this issue Aug 24, 2023 · 4 comments
Closed

Read the mail body #130

aymenbouhaha opened this issue Aug 24, 2023 · 4 comments

Comments

@aymenbouhaha
Copy link

i want to ask how to get the body of the email , i mean the content of the email

@andris9
Copy link
Collaborator

andris9 commented Sep 26, 2023

You can download the email with the download method

@andris9 andris9 closed this as completed Sep 26, 2023
@TheGP
Copy link

TheGP commented Mar 13, 2024

It doesnt work

for await (const message of client.fetch('*', { envelope: true, bodyParts: true,
            bodyStructure: true })) { // 1:*

            const { content } = await client.download(message.uid, '1.2');
            console.log('content', content)
}

no output

@andris9
Copy link
Collaborator

andris9 commented Mar 15, 2024

You can not use IMAP commands while in the async for loop. The await client.download() will have to wait until the previous for await...fetch() has been completed, and you end up with a deadlock.

@jramassamy
Copy link

Hey, as a complete noob also I had some issues understanding everything

Here is what I did (in a typescript class)

import { ImapFlow, ListResponse, FetchMessageObject, Logger } from 'imapflow';
import { ParsedMail, simpleParser } from 'mailparser';

  concat_RS = (stream) => new Promise((res, rej) => {
    var buffers = [];
    stream.on("data", function (data) { buffers.push(data); });
    stream.on("end", function () { res(Buffer.concat(buffers)); });
  });

  async fetchMessagesBody(client: ImapFlow, messages: FetchMessageObject[]) {
    // here you already fetched your messages with bodyStructure: true once
    let mailbox = await client.getMailboxLock('INBOX');
    let fullMessages: { uid: number, source: ParsedMail, text: string }[] = [];

    for (let message of messages) {
      for (let bs of messages[0].bodyStructure.childNodes) {
        if (bs.type?.toLowerCase() === 'text/plain') {
          let { meta, content } = await client.download(messages[0].seq.toString(), bs.part);
          const buf = await this.concat_RS(content);
          const contentString = buf.toString();
          const parsedMail: ParsedMail = await simpleParser(content);
          fullMessages.push({ uid: message.uid, source: parsedMail, text: contentString  });
        }
      }
    }
    mailbox.release();
    return fullMessages;
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants