Find and retrieve content within emails

MailSlurps dashboard and API offer numerous ways to filter and extract content and attachments from emails.

You can also use MailSlurp to find patterns in email contents and extract text matches for later use. Examples might be testing user sign up verification processes or customized transactional emails.

Extracting content

You can use the email controller getEmailContentMatch method to extract matching text from an email.

it('get email content', async () => {
  const mailslurp = new MailSlurp(config);

  const inbox1 = await mailslurp.createInbox();
  const inbox2 = await mailslurp.createInbox();

  const to = [inbox2.emailAddress!];
  const body = 'Hi there. Your code is: 123456';
  await mailslurp.sendEmail(inbox1.id!, { to, body });

  // wait for email
  const email = await mailslurp.waitController.waitForLatestEmail({
    inboxId: inbox2.id,
    timeout: timeoutMillis,
    unreadOnly: true,
  });
  const pattern = 'code is: ([0-9]{6})';
  expect(email.body).toContain('Your code is');

  const result = await mailslurp.emailController.getEmailContentMatch({
    contentMatchOptions: { pattern },
    emailId: email.id!,
  });
  expect(result.matches).toHaveLength(2);
  expect(result.matches[0]).toEqual('code is: 123456');
  expect(result.matches[1]).toEqual('123456');
  // now do something with the code
});
You can use MailSlurp in any language using official SDKs or in the online web app.

Matching for email fields

Say we send a verification email with a code we want to extract:

const inboxController = new InboxControllerApi(new Configuration({ apiKey }));
const inbox = await inboxController.createInbox({});
await inboxController.sendEmail({
  inboxId: inbox.id!,
  sendEmailOptions: {
    to: [inbox.emailAddress!],
    subject: 'Verification code',
    html: true,
    body: 'Here is <a href="https://test.com/verfiy?code=123">your code</a>',
  },
});

We can use waitForMatchingFirstEmail methods to extract the code.

const waitForController = new WaitForControllerApi(
  new Configuration({ apiKey })
);
const confirmation = await waitForController.waitForMatchingFirstEmail({
  inboxId: inbox.id,
  unreadOnly: true,
  timeout: 30000,
  matchOptions: {
    // pass an array of MatchOption items
    matches: [
      {
        field: MatchOptionFieldEnum.SUBJECT,
        should: MatchOptionShouldEnum.CONTAIN,
        value: 'Verification code',
      },
    ],
  },
});
// use a regex in code to extract the content
const r = /href="(https[^"]+)"/gs;
const results = r.exec(confirmation.body!!)!!;
const url = decodeURIComponent(results[1]);
// now you can use the verification code
expect(url).toBeTruthy();

Match definitions

Here are the fields and should options that can be used when matching emails:

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

Use the getEmailLinks method on the EmailController to parse emails and extract HTML links.

test('can extract email links', async () => {
  const mailslurp = new MailSlurp({ apiKey });
  // send html email to inbox 1 containing a link
  const inbox = await mailslurp.createInbox();
  const href = 'https://test.com?code=123';
  const body =
    '<html><body><p>Here is <a href="' +
    href +
    '">your link</a></p></body></html>';
  await mailslurp.sendEmail(inbox.id!!, {
    to: [inbox.emailAddress!!],
    body,
    isHTML: true,
  });
  // can get link from email
  const email = await mailslurp.waitForLatestEmail(inbox.id!!, 60000, true);
  const linkResult = await mailslurp.emailController.getEmailLinks({
    emailId: email.id!!,
  });
  expect(linkResult.links).toEqual([href]);
});

test('attachments', async () => {
  const mailslurp = new MailSlurp({ apiKey });
  //<gen>upload_attachment_inline
  const attachmentContent = 'test';
  const base64Contents = Buffer.from(attachmentContent).toString('base64');
  // notice array is returned and first element is attachment id
  const [attachmentId] = await mailslurp.uploadAttachment({
    base64Contents,
    contentType: 'text/plain',
    filename: 'test.txt',
  });
  
If you have trouble with matches try asserting the content of the email using the less strict waitForLatestEmail methods.