You can generate email previews from your automated tests with the Mailosaur API. For example, each time your tests run, they can capture your password reset email in Outlook, Gmail and Apple Mail.
Before you begin
- The Previews API is available on the Core and Enterprise plans with the Email Previews add-on. It is not available during a free trial. See Billing and plans.
- You need an API key.
- Each preview uses one credit. See Usage limits.
The examples below assume you already have a basic automated test that finds an email in Mailosaur. To create one, see our getting started guides.
How it works
- Your test sends an email to a Mailosaur inbox.
- Your test finds the email in the inbox, and gets its
id. - Your test asks Mailosaur to generate previews of the email in the email clients you choose.
- Your test downloads each preview as a PNG image.
Choose email clients
Each email client has a label. The label is made up of the device, the email client, light or dark mode, and the orientation. For example:
windows-outlook-lightmode-landscapeis Outlook on Windowsiphone-16-applemail-darkmode-portraitis Apple Mail on iPhone 16, in dark modeandroid-15-gmail-lightmode-portraitis Gmail on Android 15
To get the label of every email client you can use, list the email clients:
const result = await mailosaur.previews.listEmailClients()
result.items.forEach(client => {
console.log(client.label) // "iphone-16-applemail-darkmode-portrait"
})For a readable list, see Supported email clients and devices.
Generate previews
Pass the id of the email, and the labels of the email clients you want:
const result = await mailosaur.messages.generatePreviews(message.id, {
emailClients: [
'windows-outlook-lightmode-landscape',
'iphone-16-applemail-darkmode-portrait',
'android-15-gmail-lightmode-portrait'
]
})
console.log(result.items.length) // 3The result contains one preview for each email client. Each preview has an id, which you use to download it, and an emailClient, which is the label of its email client.
The request fails, and Mailosaur generates no previews, if:
- A label is not valid. The error suggests a valid label where it can.
- The previews would take you over your monthly limit.
Download previews
Previews take a short time to render. The download method waits until a preview is ready, then returns it as a PNG image. This example saves each preview to a file named after its email client:
const fs = require('fs')
// ...
for (const preview of result.items) {
const file = await mailosaur.files.getPreview(preview.id)
fs.writeFileSync(`${preview.emailClient}.png`, file)
}The download method waits up to 2 minutes for each preview. If a preview is not ready in that time, the method fails with a preview_timeout error.
See your previews in the Dashboard
Previews you generate with the API also appear in the Dashboard. Open the email in its inbox, and select Previews in the email viewer toolbar. From there, you can compare two previews side by side.
When an email is deleted, its previews are deleted too.
See also
- Generate email previews in the API reference
- Download an email preview in the API reference
- Supported email clients and devices