Find and retrieve content within emails

How to match email content and use regex or server side matching to extract patterns. Capture links and verification codes from emails.

  • Table of contents

extract email body content

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(!, { to, body });

  // wait for email
  const email = await mailslurp.waitController.waitForLatestEmail({
    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 },
  expect(result.matches[0]).toEqual('code is: 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({
  sendEmailOptions: {
    to: [inbox.emailAddress!],
    subject: 'Verification code',
    html: true,
    body: 'Here is <a href="">your code</a>',

We can use waitForMatchingFirstEmail methods to extract the code.

const waitForController = new WaitForControllerApi(
  new Configuration({ apiKey })
const confirmation = await waitForController.waitForMatchingFirstEmail({
  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

Match definitions

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

export enum MatchOptionFieldEnum {
    TO = 'TO',
    BCC = 'BCC',
    CC = 'CC',
    FROM = 'FROM'
export enum MatchOptionShouldEnum {

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 = '';
  const body =
    '<html><body><p>Here is <a href="' +
    href +
    '">your link</a></p></body></html>';
  await mailslurp.sendEmail(!!, {
    to: [inbox.emailAddress!!],
    isHTML: true,
  // can get link from email
  const email = await mailslurp.waitForLatestEmail(!!, 60000, true);
  const linkResult = await mailslurp.emailController.getEmailLinks({

test('attachments', async () => {
  const mailslurp = new MailSlurp({ apiKey });
  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({
    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.

More examples

Find more source code in our examples section or the guides page