ESC
NextStarterAI logo
Menu
On this page

Features > Email

✉️ Mailgun

While Mailgun is an option, any email tool will suffice for setting up features like magic login links, abandoned cart emails, etc.

Setup

Tips

Mailgun discreetly removed the "pay-as-you-go" flex tier from their pricing page, but it's still available. Begin a free trial for the $35 tier, then cancel it. You'll be reverted to the "pay-as-you-go" free tier. If you send fewer than 1000 emails per month, the cost will be $1 per month.

Opting for Resend? Check out an outstanding tutorial crafted by another indie hacker.

1. Create a new account on Mailgun


2. Within the [Sending] section, select [Domains], and then click [Add New Domain]. It's advisable to include a subdomain, such as mail.yourdomain.com.


Tips

When choosing the EU region, insert the following URL into the Mailgun client setup within libs/mailgun.ts, placed directly under username: "api":

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

3. Complete all the DNS verification procedures. If you're using a subdomain, ensure it's accurately represented in your DNS records.


4. Add extra DMARC for better deliverability: TXT | _dmarc.mail.yourdomain.com | v=DMARC1; p=none


5. Navigate to [Domain Settings], followed by [SMTP Credentials], then opt for [Reset Password]. Select [Automatic] and proceed to [Create Password].


6. Press [Copy] located at the lower-right corner of the modal. In .env.local, configure EMAIL_SERVER to: smtp://postmaster@[mail.yourdomain.com]:[copied_password]@smtp.mailgun.org:587 (excluding the brackets).


7. In the [Sending API Keys] section, hit [Create sending key], and then put it into .env.local as MAILGUN_API_KEY.


(Skip if you don't need to get emails) Within [Receiving], choose [Create Route]. Pick [Match Recipient] and insert the email you intend to send from (e.g., name@mail.yourdomain.com). Ensure it matches the email configured at mailgun.supportEmail in the config.ts file.


Forward it to https://[your-domain].com/api/webhook/mailgun. Click [Create Route]. Next, add your receiving email (e.g., using my Gmail) to mailgun.forwardRepliesTo in the config.ts.


Tips

Check your records are valid on MxToolbox (enter your subdomain if you used one)

Sending Emails


There are two methods for sending emails:


1. SMTP: Emails for magic login links, for example, will be sent through SMTP.


2. Mailgun API: For sending any other types of emails, utilize the sendEmail() function found in libs/mailgun.js.


Receiving emails


Mailgun doesn't automatically forward or store emails.


That's why we set up a way for emails sent to our support email (mentioned in config.ts) to be sent to our API (your-domain.com/api/webhook/mailgun). After that, they're forwarded to the email we specified in forwardRepliesTo in config.ts.


It automatically includes the sender in the reply-to, allowing you to respond directly from your inbox.


Checklist to avoid spam

✅ Send from a subdomain (i.e. mail.yourdomain.com)

✅ Add an SPF record (in subdomain)

✅ Add an DKIM record (in subdomain)

✅ Add an DMARC record (in subdomain)

✅ Add an ubsubscribe link at the bottom

✅ Personalize the email with recipent name

❗ Avoid spam-trigger words like free or earn money

❗ Avoid uncommon fonts, strange layout or extreme punctuation

❗ Avoid links to non-reputable websites