Get emails from code (how to receive emails programmatically)
How to receive emails directly in code, applications, and tests. Use MailSlurp to send and receive SMTP emails.
Once you have created an inbox any emails sent to its email address received and saved under the inboxes ID. MailSlurp allows you to fetch and receive emails send to an inbox's email address in several ways:
Options
Visual dashboard
The simplest way to receive emails is to create an inbox and send it an email using your email provider. You can then view the emails in your dashboard.
Individual emails
If you know that an email exists and you have its id
simply call a getEmail(emailId)
method to get a full email object that includes the body.
const [email] = await mailslurp.waitForEmailCount(1, inbox.id, 30000, true);
const { id, to, from, subject, isHTML, attachments } =
await mailslurp.getEmail(email.id!);
WaitFor methods
For programmatic use-cases the MailSlurp API and SDKs provide WaitFor methods. This is the recommended way to receive emails.
WaitFor methods hold a connection until a condition is met. If the condition is met a list of matching EmailPreview
entities is returned. These contain email id
values that can be used to fetch individual emails.
There are several waitFor
variations:
Wait For Latest
const latestEmail = await mailslurp.waitForLatestEmail(
inbox.id,
30000,
unreadOnly
);
Wait For Email Count
const emails = await mailslurp.waitForEmailCount(
1,
inbox.id,
30000,
unreadOnly
);
Wait For Matching Emails
const matchingEmails = await mailslurp.waitForMatchingEmails(
{
// match for emails with no attachments
conditions: [
{
condition: ConditionOptionConditionEnum.HAS_ATTACHMENTS,
value: ConditionOptionValueEnum.FALSE,
},
],
// match for emails from a specific email address
matches: [
{
field: MatchOptionFieldEnum.FROM,
should: MatchOptionShouldEnum.CONTAIN,
value: inbox.emailAddress,
},
],
},
1,
inbox.id,
timeout,
unreadOnly
);
Wait For Nth Email
const nthEmail = await mailslurp.waitForNthEmail(
inbox.id!,
0,
timeout,
unreadOnly
);
All Emails
You can fetch all existing emails from an inbox with the getEmails
methods. These methods return a collection of List<EmailPreview>
.
await mailslurp.getEmails(inbox.id!);
// or paginated
await mailslurp.getAllEmails(0, 10, [inbox.id!], 'DESC');
To get the body or attachments for an email you must call getEmail
or getAttachment
methods individually.
Webhooks
WebHooks are a way to be notified via HTTP POST when a message is received. If you attach a WebHook to an inbox the WebHook URL will be notified when a new email is received. WebHooks are useful for high throughput applications or for environments where WaitFor methods are not appropriate.
Receiving attachments
Whenever MailSlurp receives email it extras attachments and stores them securely to disk. Attachments are not returned directly when emails are fetched. Instead you must obtain the attachment IDs from an email and then call attachment methods to receive attachment metadata and binary content.
// get attachment content as HTTP OctetStream
downloadAttachment(emailId: string, attachmentId: string): Promise<Response>
interface AttachmentMetaData {
contentLength: number;
contentType: string;
name: string;
}