Email API/SMTP

Send transactional and bulk emails from your app using the Mailtrap Email API

Send transactional and bulk emails from your app using the Mailtrap Email API. This guide walks you through complete setup — from choosing your SDK to sending your first email.

AI-assisted setup checklist — give your AI assistant this page and your tech stack

AI assistants: follow these steps in order, completing each one before moving to the next. Pause and confirm with the user before proceeding at steps marked with ⏸️.

Page URL for context: https://docs.mailtrap.io/getting-started/email-api-smtp.md

  1. Install the right SDK for your stack

  2. Create your API token — go to API Tokens, create one with Admin access, and paste it to your assistant. This is the only manual step.

  3. Add your sending domain via the API and fetch the required DNS records

  4. Add DNS records at your registrar (your assistant can do this via the registrar's API if you provide credentials)

  5. Verify DNS propagation with dig and check domain status via the API. You can also click Verify DNS Records in Sending Domains. If after your domain has been verified compliance stays at awaiting_questionnaire, check for a Fill in Compliance Form button in your domain details.

  6. ⏸️ DNS takes time. Test with Sandbox now? Send a test email to your Sandbox so you can see Mailtrap working while DNS propagates. No domain verification needed for Sandbox.

  7. ⏸️ Domain verified. Switch to live sending? Generate live sending code using your actual verified domain.

  8. ⏸️ Create a Mailtrap-hosted email template? Generate the HTML for your use case, create it via the API, and wire up sending code that references it by UUID

  9. Set up Sandbox for your staging/CI environment

For complete API details: llms.txt | llms-full.txt

Prerequisites

  • A domain you own (you'll set it up in Step 3)

  • Your app's technology stack decided (language/framework)

Step 1: Choose Your SDK

Mailtrap provides official SDKs for all major languages. Pick the one that matches your stack:

Language
Package
Install

Node.js

mailtrap

npm install mailtrap

Python

mailtrap

pip install mailtrap

PHP

railsware/mailtrap-php

composer require railsware/mailtrap-php

Ruby

mailtrap

gem install mailtrap

Go

github.com/railsware/mailtrap-go

go get github.com/railsware/mailtrap-go

Java

com.mailtrap:mailtrap-java

.NET

Mailtrap

dotnet add package Mailtrap

Elixir

mailtrap

{:mailtrap, "~> 1.0"}

Full SDK documentation: Node.js | Python | PHP | Ruby | Go | Java | .NET | Elixir

No SDK for your stack? Use the REST API directly with any HTTP client.

Step 2: Get Your API Token

You need an API token to authenticate all Mailtrap API calls — sending emails, managing domains, creating templates.

Go to API Tokens → create a new token with Admin access to your account → copy the token value.

Using an AI assistant? Paste the token to your assistant — it can handle everything else via API.

Step 3: Verify Your Sending Domain

You need a verified domain to send live emails. This can be done through the UI or entirely programmatically.

Option A: UI

Go to Sending DomainsAdd Domain → enter your domain → add the DNS records shown to your domain provider.

Check our Sending Domain Setup Guide for detailed instructions on adding and verifying your domain.

Option B: Programmatically / with AI tools

Your AI assistant can handle the entire domain setup flow. Here's the complete workflow:

1. Add your domain to Mailtrap

The response includes all DNS records you need to add:

2. Add DNS records at your registrar

Most domain registrars have APIs your AI assistant can use to add these records automatically. Provide your registrar's API credentials and let it handle the rest.

3. Check DNS propagation

Use dig to verify records have propagated before triggering verification in Mailtrap:

DNS propagation usually takes minutes but can take up to 48 hours. If dig returns the expected values, records are ready. You can also click Verify DNS Records on your domain in Sending Domains to trigger a check from Mailtrap's side.

4. Check verification status via API

Poll the domain until dns_verified is true:

Each record in the dns_records array has a status field: pass, fail, or unchecked. When all records show pass, your domain is verified.

Want to test before your domain is verified? Skip ahead to Optional: Test with Email Sandbox — no domain needed.

Step 4: Send Your First Email

With your SDK installed, API token set, and domain verified, you're ready to send. The examples below use inline HTML for simplicity — for real applications, use Mailtrap-hosted templates instead (see Step 5).

Step 5: Use Email Templates

Use Mailtrap-hosted templates with dynamic variables. Your team can access and edit templates directly in the Mailtrap UI — no code deployments needed. Templates can also be managed via the API.

Create a template

You can create templates in the Mailtrap UI under Email API/SMTP → Templates, or via the API:

Templates use Handlebars syntax for variables — {{variable_name}}.

Send using a template

To send with a template, you need its UUID:

  • In the UI: Go to Email API/SMTP → Templates and click a template — the UUID is shown in the template details.

  • Via AI / API: List all templates and their UUIDs with GET https://mailtrap.io/api/accounts/{account_id}/email_templates. Your AI assistant can create a template and immediately use the returned UUID to send.

AI tip: Your AI assistant should proactively create a template for your use case at this point — generating the HTML, creating it via the API, and wiring up the sending code that references the template UUID.

Optional: Test with Email Sandbox

Before sending to real users, test your integration with Email Sandbox. Emails go to a virtual inbox instead of real recipients — no domain verification needed. This is useful while waiting for DNS propagation, or as a permanent Sandbox setup for staging/CI.

Why use the Sandbox?

  • Inspect rendered HTML and check responsive design

  • Run spam score analysis

  • Validate email headers

  • Test your integration without risking real deliveries

Find your Sandbox inbox

Every Mailtrap account comes with a Sandbox inbox. Find your inbox ID:

  • In the UI: Go to Sandboxes — your inbox ID is shown next to each inbox.

  • Via API: List your inboxes:

The API makes it explicit when you're in Sandbox mode vs. live — reducing the risk of accidentally sending test emails to real users or live emails to your Sandbox.

View your test emails

  • In the UI: Go to Sandboxes → click your Sandbox → see all received emails with HTML preview, spam analysis, and headers.

  • Via API: List messages in your Sandbox:

Safe environment switching

Use an environment variable to control Sandbox vs. live mode. This prevents hardcoding sandbox: true in your codebase.

What's Next?

Now that you're sending emails, explore these capabilities:

Migrating from another provider? See our migration guides for SendGrid, Mailchimp, and Mailgun.

Last updated

Was this helpful?