This guide explains how to integrate the newsletter system with real email services to send actual emails to subscribers.
The newsletter system is currently implemented with:
- β Firebase Firestore for storing subscribers and notifications
- β React components for subscription forms and admin management
- β Mock email service for demonstration purposes
- β Automatic notifications when new blogs are posted
-
Sign up for SendGrid:
- Go to sendgrid.com
- Create a free account (100 emails/day free)
-
Install SendGrid:
npm install @sendgrid/mail
-
Update EmailService:
// src/utils/emailService.js import sgMail from '@sendgrid/mail'; // Initialize SendGrid sgMail.setApiKey(process.env.REACT_APP_SENDGRID_API_KEY); export class EmailService { static async sendBlogNotification(to, blogData) { try { const emailContent = this.generateEmailContent(blogData); const msg = { to: to, from: 'your-verified-sender@yourdomain.com', // Must be verified subject: emailContent.subject, html: emailContent.html, }; await sgMail.send(msg); return true; } catch (error) { console.error('Error sending email:', error); return false; } } }
-
Environment Variables:
REACT_APP_SENDGRID_API_KEY=your_sendgrid_api_key_here
-
Sign up for Mailgun:
- Go to mailgun.com
- Create a free account
-
Install Mailgun:
npm install mailgun.js
-
Update EmailService:
// src/utils/emailService.js import formData from 'form-data'; import Mailgun from 'mailgun.js'; const mailgun = new Mailgun(formData); const mg = mailgun.client({ username: 'api', key: process.env.REACT_APP_MAILGUN_API_KEY, }); export class EmailService { static async sendBlogNotification(to, blogData) { try { const emailContent = this.generateEmailContent(blogData); const msg = { from: 'your-verified-sender@yourdomain.com', to: to, subject: emailContent.subject, html: emailContent.html, }; await mg.messages.create('yourdomain.com', msg); return true; } catch (error) { console.error('Error sending email:', error); return false; } } }
-
Set up AWS SES:
- Go to AWS Console β SES
- Verify your domain or email
- Create SMTP credentials
-
Install AWS SDK:
npm install @aws-sdk/client-ses
-
Update EmailService:
// src/utils/emailService.js import { SESClient, SendEmailCommand } from '@aws-sdk/client-ses'; const sesClient = new SESClient({ region: 'us-east-1', // Your SES region credentials: { accessKeyId: process.env.REACT_APP_AWS_ACCESS_KEY_ID, secretAccessKey: process.env.REACT_APP_AWS_SECRET_ACCESS_KEY, }, }); export class EmailService { static async sendBlogNotification(to, blogData) { try { const emailContent = this.generateEmailContent(blogData); const command = new SendEmailCommand({ Source: 'your-verified-sender@yourdomain.com', Destination: { ToAddresses: [to], }, Message: { Subject: { Data: emailContent.subject, }, Body: { Html: { Data: emailContent.html, }, }, }, }); await sesClient.send(command); return true; } catch (error) { console.error('Error sending email:', error); return false; } } }
-
Install Nodemailer:
npm install nodemailer
-
Update EmailService:
// src/utils/emailService.js import nodemailer from 'nodemailer'; const transporter = nodemailer.createTransporter({ host: 'smtp.gmail.com', // or your SMTP host port: 587, secure: false, auth: { user: process.env.REACT_APP_SMTP_USER, pass: process.env.REACT_APP_SMTP_PASS, }, }); export class EmailService { static async sendBlogNotification(to, blogData) { try { const emailContent = this.generateEmailContent(blogData); const mailOptions = { from: process.env.REACT_APP_SMTP_USER, to: to, subject: emailContent.subject, html: emailContent.html, }; await transporter.sendMail(mailOptions); return true; } catch (error) { console.error('Error sending email:', error); return false; } } }
Create a .env file in your project root:
# SendGrid
REACT_APP_SENDGRID_API_KEY=your_sendgrid_api_key_here
# OR Mailgun
REACT_APP_MAILGUN_API_KEY=your_mailgun_api_key_here
# OR AWS SES
REACT_APP_AWS_ACCESS_KEY_ID=your_aws_access_key
REACT_APP_AWS_SECRET_ACCESS_KEY=your_aws_secret_key
# OR SMTP
REACT_APP_SMTP_USER=your_email@gmail.com
REACT_APP_SMTP_PASS=your_app_password- Email validation
- Duplicate prevention
- Subscription/unsubscription
- Welcome emails
- Unsubscribe confirmation emails
- Automatic notifications when new blogs are posted
- Beautiful HTML email templates
- Blog metadata (title, excerpt, reading time, category)
- Direct links to blog posts
- View all subscribers
- Manage subscriptions
- Notification history
- Statistics and analytics
- Real-time feedback
- Loading states
- Error handling
- Success messages
- Responsive design
-
Subscribe to Newsletter:
- Go to the homepage
- Enter your email in the newsletter section
- Check console for email simulation logs
-
Create a New Blog:
- Go to admin panel
- Create a new blog post
- Check console for notification logs
-
View Admin Dashboard:
- Access newsletter management
- View subscribers and notifications
The email templates include:
- β Responsive design
- β Professional styling
- β Blog metadata
- β Call-to-action buttons
- β Unsubscribe links
- β Branded header/footer
- Email Validation: Server-side validation
- Rate Limiting: Prevent spam subscriptions
- Unsubscribe Links: Easy opt-out mechanism
- Data Privacy: GDPR compliance
- API Security: Secure API keys
Modify the HTML templates in EmailService.generateEmailContent() to match your brand.
Update the CSS in email templates for custom colors and fonts.
Customize email content, subject lines, and messaging.
Add analytics tracking:
// Track email opens, clicks, etc.
const trackEmailEvent = (event, subscriberId) => {
// Integrate with Google Analytics, Mixpanel, etc.
};- Set up environment variables in your hosting platform
- Verify sender email with your email service
- Test the system with real emails
- Monitor delivery rates and engagement
For issues or questions:
- Check the console for error logs
- Verify API keys and credentials
- Test with a single email first
- Check email service documentation
Note: The current implementation uses mock email sending for demonstration. Replace the mock implementation with your chosen email service for production use.