Mailtrap and Replit Integration

In this guide, you'll set up production email sending in a Replit project using the Mailtrap Email API with official SDKs (Node.js or Python), and learn how to create or update contacts in Mailtrap directly from your app.

You’ll:

  • Send emails from your verified domain via the Mailtrap Email API
  • Store contacts for campaigns and automations, with optional custom fields and list membership

Technical note: This tutorial uses the Replit Agent product.

Prerequisites

  • Verified sending domain in Mailtrap (required for production) — Setup guide
  • Admin API token with access to your verified domain and Contacts
  • Account ID (needed for Contacts) — find it here
  • Custom fields created in Mailtrap if you want to store extra structured data — create here
  • (Optional) List ID if you want to assign contacts to a list
  • Replit account and project (Node.js or Python)

Ready-made prompts (for Replit Agent)

Sending only

Create a Node.js app that uses the Mailtrap SDK to send a production email via the Email API using a verified domain. Store the API token and from address in environment variables.

Important note: To complete the project, Replit asks you to provide Mailrap API token and the FROM_ADDRESS. So please make sure to add and verify a domain first, as described below. 

Contacts management only

Write a Node.js script that uses the Mailtrap SDK to create or update a contact in Mailtrap. Include email, name, and custom fields. Store Account ID, API token, and optional List ID in environment variables.

Important note: To complete the project, Replit asks you to provide MAILTRAP_ACCOUT_ID token and the MAILTRAP_LIST_ID. 

Step-by-step setup

Step 1 - Set up Mailtrap

  1. Verify your sending domain in Mailtrap.
  2. Create an admin API token and keep it secure.
  3. Note your FROM_ADDRESS (must use the verified domain).

Step 2 — Create/open your Replit project

With Replit Agent - Use one of the prompts above. Replit does it all, you just need to add the credentials.

Manual setup:

  • Install SDKs:

Node.js:

npm install mailtrap

Python:

pip install mailtrap
  • Add secrets in Replit (🔑):
MAILTRAP_API_TOKEN = <your_admin_api_token>

Step 3 - Send a production email

Node.js 

import { MailtrapClient } from "mailtrap";

const client = new MailtrapClient({ token: process.env.MAILTRAP_API_TOKEN });

await client.send({
  from: { email: "no-reply@your-verified-domain.com", name: "Your App" },
  to: [{ email: "recipient@example.com", name: "Recipient" }],
  subject: "Welcome to Our App",
  text: "Hello from Mailtrap + Replit!"
});

console.log("Production email sent.");

Python

import mailtrap as mt
import os

mail = mt.Mail(
    sender=mt.Address(email="no-reply@your-verified-domain.com", name="Your App"),
    to=[mt.Address(email="recipient@example.com", name="Recipient")],
    subject="Welcome to Our App",
    text="Hello from Mailtrap + Replit!"
)

client = mt.MailtrapClient(token=os.environ["MAILTRAP_API_TOKEN"])
client.send(mail)
print("Production email sent.")

Step 4 - Run and verify

  1. Click Run in Replit. (Note: Most of the time, the AI IDE does it for you.)
  2. Check the recipient’s inbox. 

Troubleshooting

  • FROM_ADDRESS rejected - Use a verified domain.
  • 401/403 errors - Ensure API token is admin-level.

Next steps

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us