This document explains how to set up custom email authentication for your Firebase sign-in links to avoid emails going to spam folders using Mailgun SMTP.
The solution generates Firebase sign-in links using the Admin SDK without sending emails automatically, then uses Mailgun SMTP via nodemailer to send the links. SMTP settings are stored in Firebase database for easy configuration.
- Client-side:
authenticateEmail()calls a Cloud Function to generate the sign-in link - Cloud Function:
generateSignInLinkuses Firebase Admin SDK to create the link - Email Service:
sendSignInEmailreads SMTP settings from/settings/emailSmtpand sends email via Mailgun SMTP
You need to add the following structure to your Firebase Realtime Database at /settings/emailSmtp:
{
"settings": {
"emailSmtp": {
"host": "smtp.mailgun.org",
"port": "465",
"user": "postmaster@mg.yourdomain.com",
"password": "your-mailgun-smtp-password",
"fromEmail": "noreply@yourdomain.com",
"fromName": "Flightbox"
}
}
}host: SMTP server hostname (for Mailgun:smtp.mailgun.org)port: SMTP port (587 for TLS, 465 for SSL, 25 for unencrypted)user: SMTP username (for Mailgun: your domain's SMTP credentials)password: SMTP password (for Mailgun: your domain's SMTP password)fromEmail: The email address that emails will be sent fromfromName: The display name for the sender
-
Create Mailgun Account: Sign up at mailgun.com
-
Add and Verify Your Domain:
- Add your domain in the Mailgun dashboard
- Add the required DNS records (MX, TXT, CNAME)
- Wait for verification
-
Get SMTP Credentials:
- Go to Domains > [Your Domain] > Domain Settings
- Find "SMTP credentials" section
- Use the provided username and password
-
Common Mailgun SMTP Settings:
Host: smtp.mailgun.org (US) or smtp.eu.mailgun.org (EU) Port: 587 (TLS) or 465 (SSL) Username: postmaster@mg.yourdomain.com Password: [from Mailgun dashboard]
Add your Mailgun SMTP settings to Firebase Realtime Database at /settings/emailSMTP with the structure shown above.
cd functions
npm install
firebase deploy --only functionsIn your webpack config or build process, make sure __FIREBASE_PROJECT_ID__ is set correctly.
- Deploy your functions and client-side changes
- Try the email authentication flow
- Check that emails are delivered to your inbox (not spam)
- Monitor Firebase Functions logs for any errors
curl -X POST https://europe-west1-YOUR-PROJECT-ID.cloudfunctions.net/generateSignInLink \
-H "Content-Type: application/json" \
-d '{"email":"test@example.com","continueUrl":"https://yourapp.com"}'curl -X POST https://europe-west1-YOUR-PROJECT-ID.cloudfunctions.net/sendSignInEmail \
-H "Content-Type: application/json" \
-d '{"email":"test@example.com","signInLink":"https://example.com","customMessage":"Test message"}'The email template is defined in functions/auth/sendSignInEmail.js. You can customize:
- Subject line: Modify
emailSubjectvariable - HTML content: Edit the
emailHtmltemplate - Plain text: Update the
emailTextcontent - Styling: Modify the inline CSS in the HTML template
- Database Security: Ensure your Firebase database rules protect the
/settingsnode from unauthorized access - SMTP Credentials: Keep your Mailgun SMTP credentials secure
- Domain Verification: Verify your sending domain with Mailgun for better deliverability
- Rate Limiting: Consider implementing rate limiting to prevent email abuse
- Link Expiration: Firebase sign-in links expire after 1 hour by default
- SMTP settings not found: Ensure
/settings/emailSMTPexists in your Firebase database - Authentication failed: Check your Mailgun SMTP credentials
- Email not delivered: Verify your domain and check Mailgun logs
- Functions error: Check Firebase Functions logs with
firebase functions:log
-
Check Database Settings: Verify the SMTP settings structure in Firebase Console
-
Monitor Function Logs:
firebase functions:log --only sendSignInEmail,generateSignInLink
-
Test SMTP Connection: The function will log connection errors if SMTP fails
-
Check Mailgun Dashboard: Monitor your Mailgun dashboard for delivery status and logs
To avoid emails going to spam, configure these DNS records for your domain:
TXT @ "v=spf1 include:mailgun.org ~all"
Add the DKIM record provided by Mailgun in your dashboard.
TXT _dmarc "v=DMARC1; p=none; rua=mailto:dmarc@yourdomain.com"
- Mailgun account set up and domain verified
- DNS records (SPF, DKIM) configured
- SMTP settings added to Firebase database
/settings/emailSMTP - Functions deployed to production
- Client-side code updated and deployed
- Email templates customized for your brand
- Test email delivery to multiple email providers (Gmail, Outlook, etc.)
- Monitor delivery rates and spam reports
- Set up monitoring/alerting for function errors
✅ No Spam Issues: Professional SMTP service with good reputation
✅ Database Configuration: Easy to update settings without redeploying
✅ Cost Effective: Mailgun has generous free tiers
✅ Reliable: Enterprise-grade email delivery
✅ Monitoring: Detailed delivery analytics in Mailgun dashboard
✅ Firebase Security: Links still generated securely by Firebase