Using Email APIs to test your app

How to send and receive email in applications and tests using on-demand email accounts.

REST API and SDKs

Email addresses on demand

// create a randomly assigned email address
const inbox = await mailslurp.createInbox()
console.log(inbox.emailAddress)

// create a custom email address with your own domain
const customInbox = await mailslurp.createInbox('user@mydomain.com')

Fetch emails in code

// hold connection open until first email found or timeout
const email = await mailslurp.waitForLatestEmail(inbox.id)
console.log(email.subject, email.body, email.attachments)

// more examples
const nthEmail = await mailslurp.waitForNthEmail(inbox.id, index)
const emails = await mailslurp.waitForEmailCountinbox(inbox.id, count)

Use pattern matching

// fetch in tests
const welcomeEmails = await mailslurp.waitForMatchingEmails({
matches: [
{
field: 'SUBJECT',
should: 'CONTAIN',
value: 'Welcome'
}
]
}, count, inbox.id)

Extract content and verify

const testInbox = await mailslurp.createInbox()
const user = await myApp.signUp(testInbox.emailAddress)

// verify that confirmation email was sent by your app
const email = await mailslurp.waitForLatestEmail(testInbox.id)

// confirm the user using the code
const [_, verificationCode] = /your code is "([0-9]{6})"/g.exec(email.body)

// do something with code like verifying an account
await myApp.confirmUser(verificationCode)

Send email attachments

// upload attachments to mailslurp
const file = fs.readFileSync(path);
const [id] = await mailslurp.uploadAttachment({
base64Contents: new Buffer(file).toString('base64'),
filename: '',
contentType: ''
})

// attach the files with id to send
mailslurp.sendEmail(inbox.id, {
to: ['test@example.org'],
attachments: [id]
})

WebHook queues

// get messages sent to your server
@PostMapping("/inbound-emails")
fun receiveEmails(emailEvent: Map<String,String>) {
// respond to event
mailSlurp.getAttachments(emailEvent["id"])
}

Send templated emails

mailslurp.sendEmail(inbox.id, {
to: ['user@example.org'],
subject: 'Hello {{name}}',
body: 'Dear {{name}}, your code is {{code}}.',
templateVars: {
name: '',
code: ''
}
})

Convenient UI

--

--

Test Email API for end-to-end with real email addresses. Support for NodeJS, PHP, Python, Ruby, Java, C# and more. See https://www.mailslurp.com for details.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
MailSlurp | Email APIs for developers

MailSlurp | Email APIs for developers

73 Followers

Test Email API for end-to-end with real email addresses. Support for NodeJS, PHP, Python, Ruby, Java, C# and more. See https://www.mailslurp.com for details.