Email Newsletter Implementation Summary
Summarises a complete email newsletter implementation including database, API, frontend, and batch sending via Resend.
What this file does
Summarises a complete email newsletter implementation including database, API, frontend, and batch sending via Resend.
When to use it
- You need to add email subscription and newsletter sending to a Next.js app
- You want to understand how to integrate Resend for batch email delivery
- You are planning the database schema and API endpoints for subscriber management
- You need a reference for testing and scaling a newsletter feature
Assumes this stack
Email Newsletter Implementation Summary
✅ Completed Features
1. Database Structure
- ✅ Added
Subscribermodel to Prisma schema - ✅ Fields: id, email (unique), isActive, subscribedAt, updatedAt
- ✅ Migration created and applied
2. Email Service (lib/email.ts)
- ✅ Resend SDK integration
- ✅ Batch email processing (25 emails per batch)
- ✅ Beautiful HTML email template with your branding
- ✅ Sequential batch processing with 1-second delays
- ✅ Error handling and logging
- ✅ Subscriber management functions
3. API Endpoints
- ✅
POST /api/subscribe- Add new email subscribers - ✅ Updated
POST /api/news- Triggers newsletter on article publish
4. Frontend Components
- ✅ Updated
SubscriptionFormcomponent to connect to API - ✅ Real-time feedback (loading, success, error states)
- ✅ Email validation
5. Documentation
- ✅ Updated README.md with email features
- ✅ Updated SETUP_GUIDE.md with detailed email setup
- ✅ Created RESEND_SETUP.md with complete Resend guide
- ✅ Updated .env.example with Resend API key
🎯 How It Works
User Subscription Flow
1. User enters email in subscription form
2. Frontend sends POST to /api/subscribe
3. Backend validates email format
4. Email saved to Subscriber table
5. User sees success message
Newsletter Sending Flow
1. Admin publishes article via admin panel
2. Article created in database
3. System triggers sendNewsletterToSubscribers()
4. Fetches all active subscribers
5. Splits into batches of 25 emails
6. Sends each batch via Resend API
7. 1-second delay between batches
8. Logs success/failure for each batch
9. Admin sees success (emails sent in background)
📧 Email Template Features
The HTML email includes:
- TechFrag header with orange branding
- Featured article image (full width, 300px height)
- Category badge (orange)
- Article title (h2)
- Article excerpt
- Author and read time
- "Read Full Article" button (orange)
- Footer with unsubscribe link
- Responsive design
- Matches website color scheme
🔧 Configuration Required
Before Testing
-
Get Resend API Key:
- Sign up at https://resend.com (free tier: 100 emails/day)
- Create API key
- Add to
.env:RESEND_API_KEY="re_xxxxxxxxxxxx"
-
Set Base URL (optional for development):
NEXT_PUBLIC_BASE_URL="http://localhost:3000" -
For Production:
- Set up custom domain in Resend
- Update
fromemail inlib/email.tsline 58:from: 'TechFrag Newsletter <newsletter@yourdomain.com>',
🧪 Testing Instructions
Test Subscription
- Visit http://localhost:3000
- Scroll to subscription form
- Enter your email
- Click "Subscribe Now"
- Check database:
npx prisma studio→ Subscriber table
Test Newsletter Email
- Make sure you have at least one subscriber
- Go to http://localhost:3000/admin/login
- Create a new article with all fields
- Click "Publish Article"
- Watch terminal for logs:
Sending newsletter to X subscribers... Processing batch 1/Y (25 emails)... ✓ Batch 1 sent successfully Newsletter delivery complete: X sent, 0 failed - Check your inbox for the email
📊 Performance & Scaling
Current Implementation
- Batch Size: 25 emails
- Delay: 1 second between batches
- Processing: Synchronous batches (sequential)
- Error Handling: Logs errors, continues with next batch
For 100 Subscribers
- 4 batches (25 each)
- ~4 seconds total
- 100 emails sent
For 1000 Subscribers
- 40 batches (25 each)
- ~40 seconds total
- 1000 emails sent
Resend Rate Limits
- Free tier: 100 emails/day, 3000/month
- Pro tier: Much higher limits
- Adjust batch size/delay if needed
🚀 Future Enhancements
Recommended Features
-
Unsubscribe Functionality
- Create
/api/unsubscribeendpoint - Update subscriber
isActiveto false - Add unsubscribe page
- Create
-
Welcome Email
- Send welcome email on subscription
- Include what to expect
- Link to latest articles
-
Email Preferences
- Daily/weekly digest option
- Category preferences
- Frequency settings
-
Email Analytics
- Track opens (Resend provides this)
- Track clicks
- A/B testing subjects
-
Job Queue
- Use Bull or BullMQ for background jobs
- Better error recovery
- Retry failed emails
-
Email Scheduling
- Schedule newsletters for specific times
- Best send time optimization
- Timezone consideration
🔒 Security Considerations
- ✅ Email validation on frontend and backend
- ✅ API key stored in environment variables
- ✅ Emails sent in background (non-blocking)
- ✅ Error handling prevents data leaks
- ⚠️ Add rate limiting to
/api/subscribe(prevent spam) - ⚠️ Add CAPTCHA for production
- ⚠️ Implement proper unsubscribe mechanism
📝 Code Files Created/Modified
New Files
lib/email.ts- Email service with Resendapp/api/subscribe/route.ts- Subscription endpointRESEND_SETUP.md- Resend setup guideEMAIL_NEWSLETTER.md- This summary
Modified Files
prisma/schema.prisma- Added Subscriber modelcomponents/SubscriptionForm.tsx- Connected to APIapp/api/news/route.ts- Added email triggerREADME.md- Added email featuresSETUP_GUIDE.md- Added email documentation.env.example- Added Resend API key
📚 Documentation
- RESEND_SETUP.md - Complete Resend setup guide
- SETUP_GUIDE.md - Full application setup
- README.md - Project overview and quick start
✨ Summary
Your newsletter platform now has a complete email system:
- Users can subscribe via the homepage form
- Admin publishes article → emails automatically sent
- Batch processing handles large subscriber lists
- Beautiful HTML emails match your branding
- All emails logged and tracked
- Ready for production (just add Resend API key)
The implementation follows best practices:
- Non-blocking email sending
- Proper error handling
- Rate limit compliance
- Scalable batch processing
- Professional email templates
Next Steps:
- Get your free Resend API key
- Add to
.envfile - Test the subscription form
- Publish a test article
- Check your inbox!
Happy newsletters! 📧
What's inside
10 sections covering completed features, flows, template, config, testing, scaling, future enhancements, security, and code files.
Change this for your project
- Replace
newsletter@yourdomain.comwith your own sender email inlib/email.tsline 58 - Replace
TechFragbranding references with your own project name - Replace
NEXT_PUBLIC_BASE_URL="http://localhost:3000"with your production URL
Where it goes
Keep it in your repository where the agent or team that needs it will read it.
Worth borrowing
- Batch email processing with 1-second delays to respect rate limits
- Sequential batch sending with error logging and continuation on failure
- Separate setup guide (RESEND_SETUP.md) for third-party service configuration
Related Documents
YoForex Platform - Technical Documentation
Documents a forex trading community platform's architecture, admin dashboard features, API endpoints, and database schema for developers maintaining or extending the system.
Project Research Summary
Documents a full architecture and phased roadmap for an AI-powered personalized newsletter and consulting platform.
Content Marketing Launch (30 Days)
Guides a solo founder or small team through a 30-day content marketing launch, from strategy to first traffic and lead generation.
📊 A/B Testing Plan for Automated Marketing Campaigns
Provides a structured A/B testing plan for email, social media, and ad campaigns with templates, sample size formulas, and automation workflows.