Skip to main content

Email Setup

This document provides an overview of setting up email services using SendGrid and Mailgun in your project. We'll cover the necessary environment variables, the functionality provided by the project, and a guide for end users to configure their accounts and obtain API keys.

SendGrid Setup

To send emails using SendGrid, follow these steps:

Setup

  1. Create a SendGrid Account:

    • Go to SendGrid and sign up for an account.
  2. Obtain API Key:

    • After signing up, navigate to the API Keys section in your SendGrid dashboard.
    • Create a new API key and make sure to copy it. You'll need it for the environment variables.
  3. Add your API key to .env.local:

    • Rename .env.example to .env.local if you haven't already.
    • Add your SendGrid API key to SENDGRID_API_KEY in .env.local.
    • Add the sender email to SENDGRID_FROM_EMAIL in .env.local.

Sending Emails

To send an email, you need to call the SendGrid endpoint in your project. Ensure that the environment variables are properly configured.

Tip: Always validate email addresses and content before sending to avoid errors and ensure deliverability.

Example of Calling the SendGrid Endpoint

const sendEmail = async () => {
try {
const response = await fetch('/api/sendgrid', {
method: 'POST',
body: JSON.stringify({
to: 'recipient@example.com',
subject: 'Test Email',
text: 'This is a test email sent from the frontend.',
}),
});
const data = await response.json();
if (response.ok) {
console.log('Email sent successfully:', data);
} else {
console.error('Error sending email:', data);
}
} catch (error) {
console.error('Error:', error);
}
};