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
Install the right SDK for your stack
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.
Add your sending domain via the API and fetch the required DNS records
Add DNS records at your registrar (your assistant can do this via the registrar's API if you provide credentials)
Verify DNS propagation with
digand 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 atawaiting_questionnaire, check for a Fill in Compliance Form button in your domain details.⏸️ 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.
⏸️ Domain verified. Switch to live sending? Generate live sending code using your actual verified domain.
⏸️ 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
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:
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"}
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 Domains → 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:
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.
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.
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:
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.
Full Sandbox guide: Getting Started with 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?

