guides
Extract email body content, links, and verification codes
Match email body content, extract links and verification codes, and use MailSlurp APIs to turn delivered emails into test assertions or workflow data.
MailSlurp's dashboard and API help teams retrieve and extract content from delivered emails.
Use this guide when you need to:
- read the body of an email in code
- extract verification codes, OTPs, and magic links
- find links in HTML email bodies
- match expected text in transactional messages
- send extracted content into tests, webhooks, or downstream workflows
For terminology and testing guidance, start with What is the body of an email?.
Quick answer
Use MailSlurp to receive the real email, then read or match the delivered body.
The usual workflow is:
- create or use a MailSlurp inbox
- trigger the app workflow that sends the email
- wait for the matching message
- inspect the plain text or HTML body
- extract the code, link, amount, order ID, or other value
- submit that value back into the test or automation flow
This is stronger than checking that the application attempted to send an email. It proves the content arrived and can be used.
Extract body content
You can use the EmailController 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
});
Use MailSlurp APIs in any language with the official SDKs or inspect received messages in the web app.
Match email fields before reading the body
Before extracting the body, filter for the correct email. For example, send a verification email with a code you 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",
}
Find links in email bodies
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]);
});
This is useful for:
- password reset links
- email confirmation links
- magic-link login
- invite acceptance links
- billing or approval links
If you have trouble with strict matches, first assert the content of the received email using less strict waitForLatestEmail methods, then tighten the filters once you know the message shape.
What to extract from the email body
| Workflow | Content to extract | Next action |
|---|---|---|
| Signup confirmation | OTP code or verification link | Submit code or open link in the test |
| Password reset | Reset link and expiry text | Validate reset flow and timeout behavior |
| Invoice email | Total, due date, invoice number, attachment name | Store or route the structured data |
| Support email | Reply body, ticket ID, attachment references | Create or update the support record |
| Campaign QA | CTA links, unsubscribe link, footer text | Verify body content before sending broadly |
For structured operational data, use AI email parsing and structured extraction or the broader Extract platform.
Related pages
More examples
Find more source code in the examples section or the guides page.