Article Preview
Learn how to integrate Mailgun with your Replit application to send transactional and marketing emails. This guide uses Replit Agent's AI-powered coding assistant to build email functionality fast.
What is Mailgun?
Mailgun is a developer-centric email API focused on high deliverability, robust analytics, and flexible integration for both transactional and marketing emails. Mailgun provides domain verification, webhook tracking, and detailed delivery reports—making it ideal for applications that need reliable email delivery.
Part 1: MVP – Basic App with Email Sending
Get a working app with Mailgun email integration in minutes using Replit Agent.
NOTE: On the Replit free plan you'll be able to get the MVP working, but for advanced features you may need a Replit Core or Teams subscription.
Step 1: Set Up Mailgun Credentials
Instructions:
- Sign up for Mailgun at mailgun.com.
- Free tier: 100 emails/day, 1 custom domain.
- Verify your sender domain by adding DNS TXT and MX records as instructed.
- Get your credentials:
- Go to Settings > API Security, create and a private API key (we’ll need this in the next step).
- Note your verified domain (e.g., mg.yourdomain.com).
Step 2: Build Form and Send Confirmation Emails
Instructions:
- Prompt Replit to Build a Form and Send Confirmation Email via Mailgun:
Build a contact form web app that:
1. Has a form collecting name, email, and message with validation
2. When submitted, automatically sends a confirmation email to the user via Mailgun
3. Uses "We've received your message" as the subject
4. The email should thank them for reaching out
5. Show a loading state while the email is sending
6. Show a success message when the email is sent
7. If email sending fails, show a friendly error message (don't break the form)
8. Include both HTML and plain-text versions of the emails. - When requested by the agent, provide your Mailgun API key and Domain in the prompt (these will be securely stored as secrets by Replit).
Test by submitting the form with your own email and verify the confirmation email arrives.
BONUS Step – Use Mailgun Validate to check for real emails
- Prompt Replit to Validate Emails before sending
Add Email address verification using Mailgun Validate to ensure we don't send to invalid emails.
- Show the user a friendly error message for invalid emails and clearly mark the Email form field as invalid
- Never “throw” or log errors for invalid emails.
NOTE: Mailgun free accounts have a limited number of validations, if you want to deploy your app to production, check out our Mailgun plan pricing.
You're Ready to Send Emails!
You now have a fully functional email integration with Mailgun running on Replit. Your contact form can:
- ✅ Collect user submissions
- ✅ Send confirmation emails via Mailgun
- ✅ Validate email addresses before sending
- ✅ Notify admins of new submissions
- ✅ Track all email activity in the Mailgun dashboard
From here, you can extend this foundation by adding form data persistence, building an admin dashboard, or scaling to newsletter broadcasts. Mailgun handles the heavy lifting of reliable delivery and analytics—you focus on your product.
Best Practices & Deployment
Email Best Practices
- Always include a plain-text version of emails.
- Send from a recognizable address (e.g., support@yourdomain.com).
- Include unsubscribe links in newsletters.
- Monitor Mailgun dashboard for bounce rates and delivery issues.
Domain Setup
- Use a dedicated subdomain (e.g., mail.yourdomain.com) for email.
- Ensure SPF/DKIM/DMARC records are added to DNS.
- Verify domain status in Mailgun dashboard.
Security
- Store Mailgun API keys only in Cloud Secrets, never in code.
- Use authentication to protect admin pages.
- Restrict user signup by domain or manual approval.
Want to Go Further?
- Add email scheduling (send at specific times).
- Track email opens and clicks via Mailgun webhooks.
- Create email automation workflows.
- Build analytics dashboard showing email performance metrics.
- Add SMS as additional communication channel.
- Implement custom email templates.
FAQ
Do I need a paid Mailgun plan?
No. Mailgun's free tier (100 emails/day) is sufficient for this tutorial and testing. Upgrade to a paid plan when you exceed the limit.
How do I verify my domain in Mailgun?
- In Mailgun dashboard, go to Domains > Add New Domain.
- Enter your domain (e.g., mail.yourdomain.com).
- Add the DNS records (TXT, MX) provided by Mailgun to your DNS provider.
- Click Verify in Mailgun. Takes 5–30 minutes.
Why are my emails going to spam?
- Domain not verified in Mailgun.
- Missing plain-text version of email.
- Sending from unrecognizable address.
- No unsubscribe link in marketing emails.
- High bounce rates (check Mailgun dashboard for fixes).
Can I send marketing and transactional emails?
Yes. This tutorial covers both: transactional (confirmations, admin replies) and marketing (newsletters). Mailgun handles both automatically.
How do I manage newsletter unsubscribes?
Mailgun automatically tracks unsubscribes when users click the unsubscribe link. You can handle Mailgun webhooks to update your subscribers table, or manually manage it.
How do I track email opens and clicks?
Mailgun provides webhook support. Prompt Replit to create webhook handlers that listen for open/click events from Mailgun and log them to a tracking table. View metrics on an analytics dashboard.
Can I customize email content?
Yes. In each Edge Function, you can customize the subject, body, and sender. Prompt Replit to adjust email text as needed.
How do I test emails without sending?
Send test emails to your own address first. Check Mailgun logs to see all send attempts. This tutorial includes all the tools you need for testing.
What if I exceed the free tier limit?
Mailgun stops sending until the next day or you upgrade to a paid plan. For production apps, plan for paid tier usage.
Troubleshooting
"Failed to send email" error
- Check MAILGUN_API_KEY and MAILGUN_DOMAIN are set correctly in Secrets.
- Verify your domain is verified in Mailgun dashboard.
- Check Mailgun Logs for detailed error messages.
- Ensure the recipient email is valid.
Emails going to spam
- Verify SPF/DKIM/DMARC records in your DNS.
- Include plain-text version.
- Use recognizable sender address.
- Check Mailgun's Deliverability Insights for tips.
Resources
Need Support?
Our Support Team here at Sinch Mailgun is happy to help! Reach out to us in the Support page of your Mailgun Control Panel, and we'll be with you shortly!