Email Reply Parser

repository·master·Indexed 41 days ago

https://github.com/crisp-oss/email-reply-parser

A Node.js library (v2.3.9) for parsing plain-text email replies to extract core content by stripping signatures, headers, and previous reply threads. It features the EmailReplyParser class for high-level extraction and the EmailParser class for detailed fragment analysis, utilizing the re2 regex engine for performance and ReDoS protection.

Tokens
2.8K
Snippets
11
Records
12
Agent score
95%

What's inside Email Reply Parser

  1. Manage RE2 support for performance and ReDoS protection

    master

    The library uses the re2 regex engine by default for better performance and protection against Regular Expression Denial of Service (ReDoS). It automatically falls back to native JavaScript RegExp if re2 is unavailable.

    If you encounter native compilation issues or wish to exclude it, you can uninstall it. If you upgrade Node.js, you may need to rebuild the re2 dependency.

    # To exclude RE2
    npm uninstall re2
    
    # To rebuild RE2 after a Node.js upgrade
    npm rebuild re2
  2. Example: Extracting content from a complex email string

    master

    This example demonstrates how parseReply strips out the signature and the quoted reply header (e.g., On DATE, NAME <EMAIL> wrote:).

    import EmailReplyParser from "email-reply-parser";
    
    const emailContent = `Hi there,
    
    I appreciate your help with this issue.
    
    Best regards,
    John
    
    On Dec 16, 2024, at 12:47 PM, Support <support@example.com> wrote:
    
    > How can we help you today?
    > 
    > Thanks,
    > Support Team`;
    
    const parser = new EmailReplyParser();
    
    console.log(parser.parseReply(emailContent));
    // Output: "Hi there,\n\nI appreciate your help with this issue."
  3. Parse email replies and extract visible text

    master

    To extract the actual message content from an email string (stripping out replies, signatures, and headers), use the EmailReplyParser class.

    Key methods:

    • read(emailString): Processes the email string.
    • getVisibleText(): Returns the cleaned text content.
    import EmailReplyParser from "email-reply-parser";
    
    const email = new EmailReplyParser().read(MY_EMAIL_STRING);
    
    console.log(email.getVisibleText());
  4. Use the EmailParser class to parse email content

    master

    The EmailParser class is the primary entry point for extracting meaningful content from email strings. It processes the text to identify and separate the actual message from quoted replies and signatures.

    When you call .parse(text), the parser:

    1. Normalizes newlines.
    2. Attempts to fix broken signatures using configured regex patterns.
    3. Iterates through the text (processed in reverse) to identify fragments.
    4. Categorizes fragments as either part of the main message, quoted text, or signatures.
    5. Returns an Email object containing a collection of Fragment objects.

    Fragments identified as signatures or quoted text are marked as isHidden: true.

    ```typescript
    import EmailParser from 'email-reply-parser/lib/parser/emailparser.js';
    
    const parser = new EmailParser();
    const email = parser.parse("Your email text here...");
    
    // The returned 'email' object contains fragments
    console.log(email.fragments);
    ```埋
  5. Use EmailReplyParser to extract email content

    master

    The EmailReplyParser class provides a high-level API to process email strings and separate the actual message content from quoted replies or signatures.

    • read(text): Returns an EmailParser instance containing the full parsed result, allowing access to both visible and quoted text.
    • parseReply(text): A convenience method that returns only the visible text (the new content added in the latest reply), stripping away quoted history.
    • parseReplied(text): A convenience method that returns only the quoted text (the previous email history/replies).
    import EmailReplyParser from 'email-reply-parser';
    
    const parser = new EmailReplyParser();
    const emailText = "Hello, this is my new message\n\n> This is the old quoted message";
    
    // Get only the new message content
    const visibleText = parser.parseReply(emailText);
    // Output: "Hello, this is my new message"
    
    // Get only the quoted history
    const quotedText = parser.parseReplied(emailText);
    // Output: "> This is the old quoted message"
    
    // Get the full parser object for more granular control
    const fullResult = parser.read(emailText);
  6. Configure quote header regex patterns in EmailParser

    master

    The EmailParser uses a list of regular expressions to identify quote headers (lines that indicate the start of a quoted reply). You can customize these patterns using setQuoteHeadersRegex to adapt to specific email client behaviors.

    • getQuoteHeadersRegex(): Returns the current list of regex patterns used for quote headers.
    • setQuoteHeadersRegex(quoteHeadersRegex): Sets a new list of regex patterns. This method supports chaining.

    Note: The parser applies these regexes to the reversed line to detect headers that typically appear at the end of a line or block.

    import EmailParser from 'email-reply-parser/lib/parser/emailparser.js';
    
    const parser = new EmailParser();
    
    // Define custom regex patterns for quote headers
    const customRegexes = [/^On .* wrote:$/, /^From: .*/];
    
    // Set the new patterns
    parser.setQuoteHeadersRegex(customRegexes);
    
    // Parse with the new configuration
    const email = parser.parse(emailText);
  7. Use the Email class to access parsed content

    master

    The Email class represents the result of a parsed email and provides methods to retrieve different parts of the message content. It is composed of an array of Fragment objects. You can use it to extract either the visible text (the new message) or the quoted text (the previous replies/history).

    // Assuming an Email instance is provided by the parser
    const email = new Email(fragments);
    
    // Get only the new content of the email
    const visibleText = email.getVisibleText();
    
    // Get only the quoted/replied content
    const quotedText = email.getQuotedText();
    
    // Get all fragments for manual inspection
    const allFragments = email.getFragments();
  8. FragmentDTO data structure

    master

    The FragmentDTO is a data transfer object representing a parsed fragment of an email. It contains metadata about the nature of the text lines within that fragment, allowing you to distinguish between the actual reply content and metadata like signatures or quoted text.

    interface FragmentDTO {
      lines: string[];      // The actual text lines in the fragment
      isHidden: boolean;    // Indicates if the fragment is hidden
      isSignature: boolean; // Indicates if the fragment is a signature
      isQuoted: boolean;    // Indicates if the fragment is quoted text
    }
  9. Email class API reference

    master

    The Email class is the primary container for parsed email data. It manages a collection of Fragment instances.

    class Email {
      /**
       * @param fragments - An array of Fragment objects
       */
      constructor(fragments: Fragment[] = [])
    
      /**
       * Returns all fragments associated with this email.
       * @returns {Fragment[]}
       */
      getFragments(): Fragment[]
    
      /**
       * Returns the text of all fragments that are NOT hidden.
       * @returns {string}
       */
      getVisibleText(): string
    
      /**
       * Returns the text of all fragments that are marked as quoted.
       * @returns {string}
       */
      getQuotedText(): string
    }
  10. The Fragment class

    master

    The Fragment class represents a specific piece of text extracted from an email during the parsing process. Each fragment carries metadata about its role within the email structure, allowing you to distinguish between the actual reply content and metadata like signatures or quoted text from previous messages.

    Key properties available via methods:

    • getContent(): Returns the raw string content of the fragment.
    • isHidden(): Returns true if the fragment is considered hidden (e.g., part of a collapsed thread).
    • isSignature(): Returns true if the fragment is identified as an email signature.
    • isQuoted(): Returns true if the fragment is part of a quoted reply (the original message being replied to).
    • isEmpty(): Returns true if the fragment contains no non-newline characters.
    • toString(): Returns the content as a string.
    // Note: Fragment is typically instantiated by the parser, not manually by the user.
    // Example of how you might interact with a Fragment instance:
    
    if (!fragment.isSignature() && !fragment.isQuoted()) {
      console.log("Actual reply content:", fragment.getContent());
    }