Emails
This guide provides step-by-step instructions to configure Mailgun for sending and receiving emails in an Angular + NestJS project. Mailgun is a powerful tool for managing transactional and marketing emails, but you can also explore alternatives like Resend.
Features
Mailgun
Mailgun enables features such as:
- Register confirmation links
- Abandoned cart reminders
- Notification emails
While Mailgun's "pay-as-you-go" tier is no longer advertised, it's still available:
- Start with the $35/month free trial.
- Cancel it to downgrade to the free "pay-as-you-go" tier.
- Send up to 1,000 emails per month for $1/month.
Setup Instructions
1. Create a Mailgun Account
- Register a new account at Mailgun.
- In the Sending section, navigate to Domains → Add New Domain.
- Use a subdomain, e.g.,
mail.yourdomain.com, for better management.
- Use a subdomain, e.g.,
2. DNS Configuration
- Follow the DNS verification steps provided by Mailgun.
- Ensure the subdomain is reflected in the DNS records.
- Add a DMARC record for improved email deliverability:
3. Configure API Region
If you're using the EU region, update the Mailgun API setup:
4. SMTP Setup
- Go to Domain Settings → SMTP Credentials.
- Reset the password, choose Automatic, and click Create Password.
- Copy the generated credentials and add the following to your
.envfile:
5. Sending API Key
- In Sending API Keys, click Create Sending Key.
- Add the generated key to
.env:
6. Enable Mailgun Module in NestJS
To integrate Mailgun functionality, ensure the MailgunModule is properly enabled in your app.module.ts file. Uncomment the following line to import the module:
Then, uncomment and register the MailgunModule in the imports array:
This configuration activates the email functionality, allowing your application to send emails through Mailgun. Ensure the environment variables for Mailgun are set correctly in your .env file.
7. Receiving Emails (Optional)
- Go to Receiving → Create Route.
- Configure the route:
- Match Recipient: Add your email (e.g.,
support@mail.yourdomain.com). - Forward: Use your API endpoint (e.g.,
https://your-domain.com/api/webhook/mailgun).
- Match Recipient: Add your email (e.g.,
- Add the webhook signing key from Sending → Webhooks to
.env: - Set the forwarding email in your app's config file:
8. Verify DNS Records
Use MXToolbox to validate your DNS records. Enter your subdomain (if used).
Sending Emails
Method 1: Mailgun API
For other email types, use a helper function (e.g., sendEmail()) to interact with Mailgun's API.
Receiving Emails
Mailgun doesn’t automatically forward or store received emails. To manage incoming emails:
- Configure a route that matches emails sent to your support address.
- Forward emails to your API endpoint and set the
reply-toheader for direct responses from your inbox.
Email Deliverability Checklist
- Verify your domain using Mailgun.
- Add SPF, DKIM, and DMARC records.
- Test your emails using tools like Mail Tester.
- Use a dedicated sending domain (e.g.,
mail.yourdomain.com).
With this setup, your Angular + NestJS app can efficiently send and receive emails using Mailgun. Ready to scale? Ensure your API keys and configurations are secured before deploying to production.