Learn how to configure and use resend/SMTP to send emails in the BuouNext project. This guide covers setting up email services, managing email templates, and ensuring reliable email delivery for notifications, verifications, and other communication needs.
This guide will walk you through integrating the Resend email service to send emails from your Next.js project.
Ensure you have a Resend account and an API key.
Add your Resend API key to your environment variables by adding it to the .env.local file:
RESEND_API_KEY=your_resend_api_keyYou need to verify your sender email address in Resend before you can send emails:
Log in to Resend. Go to the Email Addresses or Domains section. Add and verify the email address you want to use as the from address. Resend will send a verification email to this address — follow the instructions in the email to complete the process.
Now, you can use resend to send an email. Note: /api/resend is protected and should be called server-side with an internal token header.
const response = await fetch('/api/resend', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-internal-token': process.env.NEXTAUTH_SECRET,
},
body: JSON.stringify({
to: '[email protected]',
subject: 'Hello from Next.js and Resend',
text: 'This is a test email from Resend!',
}),
});
const result = await response.json();
if (result.success) {
console.log('Email sent successfully:', result.email);
} else {
console.error('Failed to send email:', result.error);
}
Ensure you have a STMP server
const transporter = nodemailer.createTransport({
host: process.env.EMAIL_SERVER_HOST,
port: Number(process.env.EMAIL_SERVER_PORT),
secure: Number(process.env.EMAIL_SERVER_PORT) === 465,
auth: {
user: process.env.EMAIL_SERVER_USER,
pass: process.env.EMAIL_SERVER_PASSWORD,
},
})
await fetch("/api/email", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
to: toArray,
title: "Welcome to BuouNext",
content: `content`,
head: "BuouNext",
}),
})