SaaS Core/Documentation

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

  1. Visit Resend website
  2. Click "Sign Up" to create an account
  3. Verify your email address
  4. Log in to Resend dashboard

2. Obtain API Key

  1. In Resend dashboard, click "API Keys" in the left menu
  2. Click "Create API Key" button
  3. Enter key name (e.g., saas-core-production)
  4. Select permissions:
    • Domain sending access: Send access
    • Webhook access: Full access (if webhooks are needed)
  5. Click "Add" to create the key
  6. Important: Copy the generated API key immediately - it will only be shown once

3. Domain Verification (Recommended)

Add Custom Domain

  1. In Resend dashboard, click "Domains"
  2. Click "Add Domain" button
  3. Enter your domain (e.g., yourdomain.com)
  4. 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

  1. Add the above records with your DNS provider
  2. Wait for DNS propagation (typically 5-30 minutes)
  3. 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:

  1. In Resend dashboard, click "Webhooks"
  2. Click "Add Webhook"
  3. Configure Webhook URL: https://yourdomain.com/api/webhooks/resend
  4. Select events to monitor:
    • email.sent
    • email.delivered
    • email.bounced
    • email.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

  1. API Key Security

    • Never hardcode API keys in code
    • Use environment variables for sensitive information
    • Regularly rotate API keys
  2. Domain Security

    • Configure DMARC policies
    • Monitor domain reputation
    • Use dedicated sending domains
  3. 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.