Matching Email Content

MailSlurp provides many API endpoints for waiting for emails and extracting their content. It also provides search and pattern matching features. These are designed to work with the unpredictable nature of email: emails may not always arrive in the order expected. Therefore by using unseenOnly: true and a collection or matchOptions with waitForMatchingX endpoints lets you wait for specific emails in an asynchronous way.

Let’s take a look at some matching examples using the Javascript SDK.

If you want to see more email and inbox operations se the [fetching email content] guide.

Match options

Email match operations typically apply a list of patterns to fields of emails (excluding the body) to return a set of matching results.

The basic definitions are so:

export enum MatchOptionFieldEnum {
    SUBJECT = 'SUBJECT',
    TO = 'TO',
    BCC = 'BCC',
    CC = 'CC',
    FROM = 'FROM'
}

export enum MatchOptionShouldEnum {
    CONTAIN = 'CONTAIN',
    EQUAL = 'EQUAL'
}

Usage example

Construct match options with multiple matches. They will be sequentially applied as filters to the emails found in an inbox. The operation will continue until the desired number of matching emails is found.

// filter for emails containing the subject "Apples". 
 const matchOptions = {
    matches: [
        {
            field: "SUBJECT",
            should: "CONTAIN",
            value: "Apples"
        }
    ]
};

Then using the Javascript SDK you can wait for expected matching emails.

const expectCount = 1;
const matchingEmails = await mailslurp.waitController.waitForMatchingEmail(matchOptions, expectCount, inbox2.id, timeoutMillis, true)

// expect the apples email to have been matched due to the subject condition
expect(matchingEmails.length).toEqual(1);
expect(matchingEmails[0].subject).toEqual("Apples");
expect(matchingEmails[0].read).toEqual(false);

Next steps