Search code examples
next.jsvercelresend.com

How do I receive Emails from Vercel deployed Next.js project using Resend? I'm not seeing any logs


I am using Resend for email sending for my contact form in my next.js project. I'm filling out the contact form and submitting, but I'm not getting the email or seeing a failed or successful log in Resend. However, I am getting a siccesful message in my Vercel deployment log. Keep in mind that I am getting emails in localhost but not after deployment. How do I fix this?

import { NextApiRequest, NextApiResponse } from 'next';
import { Resend } from 'resend';

const resend = new Resend(process.env.RESEND_API_KEY);

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  if (req.method !== 'POST') {
    return res.status(405).json({ message: 'Method Not Allowed' });
  }

  try {
    const formData = req.body;

    console.log('Received form data:', formData);

    resend.emails.send({
      from: '[email protected]',
      to: '[email protected]',
      subject: 'New Contact Form Message',
      html: `<p>This is an Email to DWS from Contact Form. the name of the person is: ${formData.name}, the referrer is ${formData.referrer}, and the email is ${formData.email}. The message is: (${formData.message})</p>`
    });

    // if resend is not sending the email, throw error
    if (!resend) {
      console.error('Error sending message');
      return res.status(500).json({ message: 'Internal Server Error' });
    }

    console.log('Message sent successfully');

    return res.status(200).json({ success: true });
  } catch (error) {
    console.error('Error sending message:', error);
    return res.status(500).json({ message: 'Internal Server Error' });
  }
}

Solution

  • You should modify your code to await the resend.emails.send call and properly handle its response or any errors it throws