DevRocket
Features

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:

  1. Start with the $35/month free trial.
  2. Cancel it to downgrade to the free "pay-as-you-go" tier.
  3. Send up to 1,000 emails per month for $1/month.

Setup Instructions

1. Create a Mailgun Account

  1. Register a new account at Mailgun.
  2. In the Sending section, navigate to DomainsAdd New Domain.
    • Use a subdomain, e.g., mail.yourdomain.com, for better management.

2. DNS Configuration

  1. Follow the DNS verification steps provided by Mailgun.
    • Ensure the subdomain is reflected in the DNS records.
  2. Add a DMARC record for improved email deliverability:
    TXT | _dmarc.mail.yourdomain.com | v=DMARC1; p=none

3. Configure API Region

If you're using the EU region, update the Mailgun API setup:

url: "https://api.eu.mailgun.net/"

4. SMTP Setup

  1. Go to Domain SettingsSMTP Credentials.
  2. Reset the password, choose Automatic, and click Create Password.
  3. Copy the generated credentials and add the following to your .env file:
    EMAIL_SERVER=smtp://postmaster@mail.yourdomain.com:[password]@smtp.mailgun.org:587
    

5. Sending API Key

  1. In Sending API Keys, click Create Sending Key.
  2. Add the generated key to .env:
    MAILGUN_API_KEY=your-mailgun-api-key
    

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:

// import { MailgunModule } from './email/email.module'; // Uncomment this line to enable the Mailgun module

Then, uncomment and register the MailgunModule in the imports array:

// imports: [MailgunModule], // Uncomment this line to enable the Mailgun module

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)

  1. Go to ReceivingCreate Route.
  2. 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).
  3. Add the webhook signing key from SendingWebhooks to .env:
    MAILGUN_SIGNING_KEY=your-webhook-signing-key
    
  4. Set the forwarding email in your app's config file:
    mailgun.forwardRepliesTo = "your-email@gmail.com";

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:

  1. Configure a route that matches emails sent to your support address.
  2. Forward emails to your API endpoint and set the reply-to header for direct responses from your inbox.

Email Deliverability Checklist

  1. Verify your domain using Mailgun.
  2. Add SPF, DKIM, and DMARC records.
  3. Test your emails using tools like Mail Tester.
  4. 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.