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.
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:
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
.NET
Mailtrap
dotnet add package Mailtrap
Elixir
mailtrap
{:mailtrap, "~> 1.0"}
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.
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 the Sending Domains page → Add 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:
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.
After DNS verification, newly added domains undergo a compliance check. Your domain's compliance_status will progress from under_review → awaiting_questionnaire → compliant. If the status stays at awaiting_questionnaire, check your domain details in Sending Domains — you may need to click Fill in Compliance Form and provide additional information about your sending practices.
Step 4: Send your first email
With your SDK installed, API token set, and domain verified, you're ready to send your first email.
The examples below use inline HTML for simplicity. For real applications, use Mailtrap-hosted templates instead (see Step 5).
For more sending code examples (and even ready-made prompts for your favorite AI assistants) visit the Sending Setup page.
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.
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:
Use the API for Sandbox (recommended)
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.
Avoid common mistakes:
Use the API (not SMTP) for Sandbox testing — the API uses a distinct base URL (
sandbox.api.mailtrap.iovs.send.api.mailtrap.io) making the environment explicit. With SMTP, you'd need to swap credentials entirely, which is error-prone.Never hardcode
sandbox: truein live code. Use environment variables.The Sandbox inbox ID is visible in the Mailtrap UI under Email Sandbox.
What's Next?
Now that you're sending emails, explore these capabilities:
Tracking Settings — Enable open and click tracking
Webhooks — Get real-time delivery event notifications
Suppressions — Manage bounces and unsubscribes automatically
Email Logs — Inspect delivery status for every email
Email Templates — Create and manage templates
Email Sandbox — Set up Sandbox for your staging environment
Last updated
Was this helpful?

