This guide will help you set up Gmail SMTP to send real emails for your newsletter system.
- ✅ Free - No monthly costs
- ✅ Reliable - Google's infrastructure
- ✅ Easy Setup - Simple configuration
- ✅ High Deliverability - Good reputation
- ✅ Secure - OAuth2 or App Passwords
- Gmail Account - You need a Gmail account
- 2-Factor Authentication - Must be enabled
- App Password - Generated for this specific use
- Go to Google Account Security
- Click on "2-Step Verification"
- Follow the setup process
- Important: This is required to generate App Passwords
- Go to App Passwords
- Select "Mail" from the dropdown
- Select "Other" as device type
- Name it "Blog Newsletter" or "TechCraft Hub"
- Click "Generate"
- Copy the 16-character password (no spaces)
Create a .env file in your project root:
# Gmail SMTP Configuration
REACT_APP_GMAIL_USER=your-email@gmail.com
REACT_APP_GMAIL_APP_PASSWORD=your-16-character-app-password
# Example:
# REACT_APP_GMAIL_USER=myblog@gmail.com
# REACT_APP_GMAIL_APP_PASSWORD=abcd efgh ijkl mnop- Start your development server
- Go to the homepage
- Subscribe to the newsletter with a test email
- Check the console for Gmail SMTP logs
- Check your email inbox for the welcome email
For production, consider using OAuth2 instead of App Passwords:
-
Google Cloud Console Setup:
- Go to Google Cloud Console
- Create a new project
- Enable Gmail API
- Create OAuth2 credentials
-
Environment Variables:
REACT_APP_GOOGLE_CLIENT_ID=your-client-id
REACT_APP_GOOGLE_CLIENT_SECRET=your-client-secret
REACT_APP_GOOGLE_REFRESH_TOKEN=your-refresh-tokenIf you want to use Nodemailer directly:
npm install nodemailerThen update the GmailService.js:
import nodemailer from 'nodemailer';
const transporter = nodemailer.createTransporter({
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: process.env.REACT_APP_GMAIL_USER,
pass: process.env.REACT_APP_GMAIL_APP_PASSWORD
}
});
// Use transporter.sendMail() instead of mock implementation- ✅ Never commit
.envfiles to version control - ✅ Use different credentials for development and production
- ✅ Rotate App Passwords regularly
- ✅ Use App Passwords, not your regular password
- ✅ Enable 2-Factor Authentication
- ✅ Monitor your Gmail account for suspicious activity
- ✅ Revoke unused App Passwords
- Gmail has daily sending limits (500 emails/day for regular accounts)
- Monitor your usage to avoid hitting limits
- Consider upgrading to Google Workspace for higher limits
-
"Invalid credentials" error:
- Make sure you're using the App Password, not your regular password
- Ensure 2-Factor Authentication is enabled
- Regenerate the App Password if needed
-
"Connection timeout" error:
- Check your internet connection
- Verify the SMTP settings (smtp.gmail.com:587)
- Try port 465 with
secure: true
-
"Authentication failed" error:
- Double-check your email and App Password
- Make sure there are no extra spaces
- Try regenerating the App Password
-
Emails not sending:
- Check the browser console for errors
- Verify environment variables are loaded
- Test with a simple email first
Enable debug logging by adding this to your .env:
REACT_APP_DEBUG_EMAILS=trueThis will show detailed logs in the console.
Add these features to monitor your newsletter:
-
Open Rate Tracking:
- Add tracking pixels to emails
- Log when emails are opened
-
Click Tracking:
- Track clicks on links in emails
- Monitor which content is most engaging
-
Bounce Rate:
- Monitor failed deliveries
- Clean up invalid email addresses
- Check Gmail's "Sent" folder for delivery confirmation
- Monitor your Gmail account for any security alerts
- Use Gmail's built-in analytics if available
For production deployment, set these environment variables:
Vercel:
vercel env add REACT_APP_GMAIL_USER
vercel env add REACT_APP_GMAIL_APP_PASSWORDNetlify:
- Go to Site Settings → Environment Variables
- Add the Gmail credentials
Firebase Hosting:
- Use Firebase Functions for email sending
- Store credentials in Firebase Functions environment
- Use Environment Variables: Never hardcode credentials
- Rotate Passwords: Change App Passwords regularly
- Monitor Usage: Watch for unusual activity
- Backup Configuration: Keep credentials in a secure location
If you encounter issues:
- Check the console for error messages
- Verify Gmail settings are correct
- Test with a simple email first
- Check Gmail's security settings
- Regenerate App Password if needed
You'll know it's working when:
- ✅ Console shows "Using Gmail SMTP for sending email..."
- ✅ Welcome emails arrive in subscribers' inboxes
- ✅ Blog notification emails are sent automatically
- ✅ No authentication errors in console
- ✅ Emails appear in your Gmail "Sent" folder
Note: This setup uses Gmail's SMTP server with App Passwords. For production applications with high volume, consider using dedicated email services like SendGrid or Mailgun for better deliverability and features.