# Automate email previews Generate email previews from your automated tests, and download each one as an image. 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](/docs/managing-your-account/billing). - You need an [API key](/docs/managing-your-account/api-keys). - Each preview uses one credit. See [Usage limits](/docs/managing-your-account/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](/docs/automation). ## How it works 1. Your test sends an email to a Mailosaur inbox. 2. Your test finds the email in the inbox, and gets its `id`. 3. Your test asks Mailosaur to generate previews of the email in the email clients you choose. 4. 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-landscape` is Outlook on Windows - `iphone-16-applemail-darkmode-portrait` is Apple Mail on iPhone 16, in dark mode - `android-15-gmail-lightmode-portrait` is Gmail on Android 15 To get the label of every email client you can use, list the email clients: ```nodejs const result = await mailosaur.previews.listEmailClients() result.items.forEach(client => { console.log(client.label) // "iphone-16-applemail-darkmode-portrait" }) ``` ```cypress cy.mailosaurListPreviewEmailClients().then(result => { result.items.forEach(client => { cy.log(client.label) // "iphone-16-applemail-darkmode-portrait" }) }) ``` ```python result = mailosaur.previews.list_email_clients() for client in result.items: print(client.label) # "iphone-16-applemail-darkmode-portrait" ``` ```java EmailClientListResult result = mailosaur.previews().listEmailClients(); for (EmailClient client : result.items()) { System.out.println(client.label()); // "iphone-16-applemail-darkmode-portrait" } ``` ```csharp var result = mailosaur.Previews.ListEmailClients(); foreach (var client in result.Items) { Console.WriteLine(client.Label); // "iphone-16-applemail-darkmode-portrait" } ``` ```ruby result = mailosaur.previews.list_email_clients result.items.each do |client| puts(client.label) # "iphone-16-applemail-darkmode-portrait" end ``` ```php $result = $mailosaur->previews->allEmailClients(); foreach ($result->items as $client) { print($client->label); // "iphone-16-applemail-darkmode-portrait" } ``` ```go result, _ := m.Previews.ListEmailClients() for _, client := range result.Items { fmt.Println(client.Label) // "iphone-16-applemail-darkmode-portrait" } ``` For a readable list, see [Supported email clients and devices](/docs/email-previews/supported-clients). ## Generate previews Pass the `id` of the email, and the labels of the email clients you want: ```nodejs 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) // 3 ``` ```cypress cy.mailosaurGenerateEmailPreviews(message.id, { emailClients: [ 'windows-outlook-lightmode-landscape', 'iphone-16-applemail-darkmode-portrait', 'android-15-gmail-lightmode-portrait' ] }).then(result => { cy.log(result.items.length) // 3 }) ``` ```python options = PreviewRequestOptions([ "windows-outlook-lightmode-landscape", "iphone-16-applemail-darkmode-portrait", "android-15-gmail-lightmode-portrait" ]) result = mailosaur.messages.generate_previews(message.id, options) print(len(result.items)) # 3 ``` ```java PreviewRequestOptions options = new PreviewRequestOptions(); options.withEmailClients(Arrays.asList( "windows-outlook-lightmode-landscape", "iphone-16-applemail-darkmode-portrait", "android-15-gmail-lightmode-portrait" )); PreviewListResult result = mailosaur.messages().generatePreviews(message.id(), options); System.out.println(result.items().size()); // 3 ``` ```csharp var options = new PreviewRequestOptions(new List { "windows-outlook-lightmode-landscape", "iphone-16-applemail-darkmode-portrait", "android-15-gmail-lightmode-portrait" }); var result = mailosaur.Messages.GeneratePreviews(message.Id, options); Console.WriteLine(result.Items.Count); // 3 ``` ```ruby options = Mailosaur::Models::PreviewRequestOptions.new([ 'windows-outlook-lightmode-landscape', 'iphone-16-applemail-darkmode-portrait', 'android-15-gmail-lightmode-portrait' ]) result = mailosaur.messages.generate_previews(message.id, options) puts(result.items.length) # 3 ``` ```php $options = new PreviewRequestOptions(array( 'windows-outlook-lightmode-landscape', 'iphone-16-applemail-darkmode-portrait', 'android-15-gmail-lightmode-portrait' )); $result = $mailosaur->messages->generatePreviews($message->id, $options); print(count($result->items)); // 3 ``` ```go result, _ := m.Messages.GeneratePreviews(message.Id, &PreviewRequestOptions{ EmailClients: []string{ "windows-outlook-lightmode-landscape", "iphone-16-applemail-darkmode-portrait", "android-15-gmail-lightmode-portrait", }, }) fmt.Println(len(result.Items)) // 3 ``` The 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: ```nodejs const fs = require('fs') // ... for (const preview of result.items) { const file = await mailosaur.files.getPreview(preview.id) fs.writeFileSync(`${preview.emailClient}.png`, file) } ``` ```cypress result.items.forEach(preview => { cy.mailosaurDownloadPreview(preview.id).then(file => { cy.writeFile(`${preview.emailClient}.png`, file, 'binary') }) }) ``` ```python for preview in result.items: file = mailosaur.files.get_preview(preview.id).content with open(preview.email_client + ".png", "wb") as f: f.write(file) ``` ```java for (Preview preview : result.items()) { byte[] file = mailosaur.files().getPreview(preview.id()); Files.write(Paths.get(preview.emailClient() + ".png"), file); } ``` ```csharp foreach (var preview in result.Items) { var file = mailosaur.Files.GetPreview(preview.Id); File.WriteAllBytes($"{preview.EmailClient}.png", file); } ``` ```ruby result.items.each do |preview| file = mailosaur.files.get_preview(preview.id) File.open("#{preview.email_client}.png", 'wb') { |fp| fp.write(file) } end ``` ```php foreach ($result->items as $preview) { $file = $mailosaur->files->getPreview($preview->id); file_put_contents($preview->emailClient . '.png', $file); } ``` ```go for _, preview := range result.Items { file, _ := m.Files.GetPreview(preview.Id) err := os.WriteFile(preview.EmailClient+".png", file, 0644) if err != nil { // handle error } } ``` 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](/docs/email-previews/compare-previews). When an email is deleted, its previews are deleted too. ## See also - [Generate email previews](/docs/api#generate-email-previews) in the API reference - [Download an email preview](/docs/api#download-an-email-preview) in the API reference - [Supported email clients and devices](/docs/email-previews/supported-clients)