Email Service Configuration
This guide provides comprehensive instructions for configuring email services for SaaS Core, including Resend email service setup and usage.
Resend Email Service Setup
1. Create Resend Account
- Visit Resend website
- Click "Sign Up" to create an account
- Verify your email address
- Log in to Resend dashboard
2. Obtain API Key
- In Resend dashboard, click "API Keys" in the left menu
- Click "Create API Key" button
- Enter key name (e.g.,
saas-core-production) - Select permissions:
- Domain sending access:
Send access - Webhook access:
Full access(if webhooks are needed)
- Domain sending access:
- Click "Add" to create the key
- Important: Copy the generated API key immediately - it will only be shown once
3. Domain Verification (Recommended)
Add Custom Domain
- In Resend dashboard, click "Domains"
- Click "Add Domain" button
- Enter your domain (e.g.,
yourdomain.com) - Click "Add" to add the domain
DNS Record Configuration
Resend will provide DNS records that need to be added, typically including:
# SPF record
TXT @ "v=spf1 include:_spf.resend.com ~all"
# DKIM record
TXT resend._domainkey "p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC..."
# DMARC record (optional but recommended)
TXT _dmarc "v=DMARC1; p=quarantine; rua=mailto:[email protected]"
Verify Domain
- Add the above records with your DNS provider
- Wait for DNS propagation (typically 5-30 minutes)
- Click "Verify" in Resend dashboard to verify the domain
4. Environment Variables Configuration
Add the following configuration to your .env.local file:
# Resend email service configuration
RESEND_API_KEY="re_xxxxxxxxxxxxxxxxxxxxxxxxxx"
SENDER_EMAIL_ADDRESS="[email protected]" # Use verified domain
SENDER_EMAIL_NAME="SaaS Core" # Sender display name
5. Email Templates Configuration
Supported Email Types
- Email Verification: Sent when users register
- Password Reset: Sent when users forget their password
- Payment Success Notification: Sent after successful subscription or purchase
- Newsletter: Marketing emails and product updates
Custom Email Templates
Email templates are implemented as React components in the codebase. You can customize:
// Example email template structure (React component)
import { Resend } from 'resend';
const resend = new Resend(process.env.RESEND_API_KEY);
// Send email verification
export async function sendVerificationEmail(email: string, verificationUrl: string) {
const { data, error } = await resend.emails.send({
from: `${process.env.SENDER_EMAIL_NAME} <${process.env.SENDER_EMAIL_ADDRESS}>`,
to: [email],
subject: 'Verify Your Email Address',
html: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<h2>Welcome to SaaS Core!</h2>
<p>Please click the link below to verify your email address:</p>
<a href="${verificationUrl}" style="background: #007bff; color: white; padding: 12px 24px; text-decoration: none; border-radius: 4px;">
Verify Email
</a>
</div>
`
});
if (error) {
console.error('Email sending failed:', error);
throw error;
}
return data;
}
Advanced Configuration
Webhook Configuration (Optional)
If you need to receive email status callbacks:
- In Resend dashboard, click "Webhooks"
- Click "Add Webhook"
- Configure Webhook URL:
https://yourdomain.com/api/webhooks/resend - Select events to monitor:
email.sentemail.deliveredemail.bouncedemail.complained
Email Sending Limits
Free Plan Limits
- 3,000 emails per month
- 100 emails per day
- Maximum 50 recipients per send
Paid Plans
- Pro: $20/month, 50,000 emails
- Scale: $85/month, 500,000 emails
Testing Email Configuration
You can create test scripts to verify email configuration:
# Create test script (optional)
cat > scripts/test-email.js << 'EOF'
const { Resend } = require('resend');
const resend = new Resend(process.env.RESEND_API_KEY);
async function testEmail() {
try {
const { data, error } = await resend.emails.send({
from: `${process.env.SENDER_EMAIL_NAME} <${process.env.SENDER_EMAIL_ADDRESS}>`,
to: ['[email protected]'],
subject: 'Test Email from SaaS Core',
html: '<p>This is a test email to verify Resend configuration.</p>',
});
if (error) {
console.error('Email sending failed:', error);
} else {
console.log('Email sent successfully:', data);
}
} catch (error) {
console.error('Error:', error);
}
}
testEmail();
EOF
# Run test
node scripts/test-email.js
Troubleshooting
1. Email Sending Failure
Problem: Receiving "Domain not verified" error Solution: Ensure domain is verified, or use Resend's default domain
2. Emails Going to Spam
Problem: Sent emails are marked as spam Solution:
- Configure SPF, DKIM, DMARC records
- Use verified domain
- Avoid spam trigger keywords
3. Invalid API Key
Problem: Receiving "Invalid API key" error Solution:
- Check if API key is correctly copied
- Confirm key permission settings
- Generate new API key
4. Sending Rate Limit
Problem: Receiving "Rate limit exceeded" error Solution:
- Check current plan sending limits
- Consider upgrading to paid plan
- Implement email queue system
Monitoring and Analytics
Email Statistics
In Resend dashboard you can view:
- Send success rate
- Open rate
- Click rate
- Bounce rate
- Complaint rate
Logging
Email sending logs in the application are located in:
- Development environment: Console output
- Production environment: Server log files
Security Best Practices
-
API Key Security
- Never hardcode API keys in code
- Use environment variables for sensitive information
- Regularly rotate API keys
-
Domain Security
- Configure DMARC policies
- Monitor domain reputation
- Use dedicated sending domains
-
Content Security
- Validate email content
- Prevent email injection attacks
- Implement email template security checks
📞 Need Help?
If you encounter issues during configuration, consult the Resend official documentation or contact technical support.