Resend Email

hey, I'm created an enquiries page for people to send an email to the enquiries email address, I want to send the email from the email address they provided to my enquiries account, is this possible? this is my nextjs API route which is using Resend,
import { env } from "@/env.mjs";
import { type ErrorResponse } from "resend";
import { z } from "zod";
import { resend } from "@/lib/resend";
import EnquiryEmail from "@/components/emails/enquiry";
import { contactSchema } from "@/lib/validations/contact";

export async function POST(req: Request) {
const input = contactSchema.parse(await req.json());

try {
await resend.emails.send({
from: input.email,
to: env.EMAIL_FROM_ADDRESS,
subject: `Enquiry from ${input.firstName} ${input.lastName}`,
react: EnquiryEmail({
firstName: input.firstName,
lastName: input.lastName,
email: input.email,
phoneNumber: input.phoneNumber,
addressLine1: input.addressLine1,
cityOrTown: input.cityOrTown,
postcode: input.postcode,
enquiry: input.enquiry,
}),
});

return new Response(null, { status: 200 });
} catch (error) {
console.error(error);

if (error instanceof z.ZodError) {
return new Response(error.message, { status: 422 });
}

const resendError = error as ErrorResponse;

if (resendError?.error?.message) {
return new Response(resendError.error.message, { status: 429 });
}

if (error instanceof Error) {
return new Response(error.message, { status: 500 });
}

return new Response("Something went wrong", { status: 500 });
}
}
import { env } from "@/env.mjs";
import { type ErrorResponse } from "resend";
import { z } from "zod";
import { resend } from "@/lib/resend";
import EnquiryEmail from "@/components/emails/enquiry";
import { contactSchema } from "@/lib/validations/contact";

export async function POST(req: Request) {
const input = contactSchema.parse(await req.json());

try {
await resend.emails.send({
from: input.email,
to: env.EMAIL_FROM_ADDRESS,
subject: `Enquiry from ${input.firstName} ${input.lastName}`,
react: EnquiryEmail({
firstName: input.firstName,
lastName: input.lastName,
email: input.email,
phoneNumber: input.phoneNumber,
addressLine1: input.addressLine1,
cityOrTown: input.cityOrTown,
postcode: input.postcode,
enquiry: input.enquiry,
}),
});

return new Response(null, { status: 200 });
} catch (error) {
console.error(error);

if (error instanceof z.ZodError) {
return new Response(error.message, { status: 422 });
}

const resendError = error as ErrorResponse;

if (resendError?.error?.message) {
return new Response(resendError.error.message, { status: 429 });
}

if (error instanceof Error) {
return new Response(error.message, { status: 500 });
}

return new Response("Something went wrong", { status: 500 });
}
}
5 Replies
max14
max14OP2y ago
or instead would I just email myself basically?
cogdancer
cogdancer2y ago
I would send from [email protected] with a reply_to param of their address. It looks like Resend supports this. https://resend.com/docs/api-reference/emails/send-email This represents the actual situation clearly: your server is sending this email, not their email provider, but replies should be addressed to their email address.
Resend
Send Email - Resend
Start sending emails through the Resend Email API.
cogdancer
cogdancer2y ago
You could also send from [email protected], but apparently it's considered best practice to send transactional emails from an address that accepts incoming mail, even if you mostly don't read it
max14
max14OP2y ago
oh thanks, i read through the docs but for some reason didn’t find this page? maybe i’m blind 😬
cogdancer
cogdancer2y ago
Took me a second to find it as well. Lots of examples, but the actual API docs aren't obvious 😅
Want results from more Discord servers?
Add your server