diff --git a/app/api/send/route.ts b/app/api/send/route.ts index 1b0812c..df33d1c 100644 --- a/app/api/send/route.ts +++ b/app/api/send/route.ts @@ -4,13 +4,14 @@ import * as React from 'react'; const resend = new Resend(process.env.RESEND_API_KEY); -export async function POST() { +export async function POST(req: Request) { + const { subject, msg, toEmail } = await req.json(); try { const { data, error } = await resend.emails.send({ from: 'Acme ', - to: ['delivered@resend.dev'], - subject: "Hello world", - react: EmailTemplate({ firstName: "John" }) as React.ReactElement, + to: [toEmail], + subject: subject, + react: EmailTemplate({ firstName: "John", msg: msg }) as React.ReactElement, }); if (error) { diff --git a/app/components/EmailForm.tsx b/app/components/EmailForm.tsx new file mode 100644 index 0000000..f1d2d52 --- /dev/null +++ b/app/components/EmailForm.tsx @@ -0,0 +1,68 @@ +'use client' +import { useState } from "react"; + +export default function EmailForm() { + const [subject, setSubject] = useState(); + const [msg,setMsg] = useState(); + const [toEmail,setToEmail] = useState(); + const [isSubmittedSucessfully, setIsSubmittedSucessfully] = useState(false); + + const handleSubmit = () => { + const emailValues = { + subject, + msg, + toEmail + }; + + const options = { + method: `POST`, + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(emailValues), + }; + + fetch(`/api/send`, options) + .then((response) => { + if (response.ok) { + setIsSubmittedSucessfully(true); + return response.json().then((data) => console.log(data)); + } + throw new Error("Api is not available"); + }) + .catch((error) => { + setIsSubmittedSucessfully(false); + console.error("Error fetching data: ", error); + }); + + } + + const handleSubject = (e:React.ChangeEvent) => { + const subject = e.currentTarget.value; + setSubject(subject); + } + + const handleMsg = (e:React.ChangeEvent) => { + const msg = e.currentTarget.value; + setMsg(msg); + } + + const handleToEmail = (e:React.ChangeEvent) => { + const toEmail = e.currentTarget.value; + setToEmail(toEmail.toString()); + } + + return
+ + {isSubmittedSucessfully &&
Success! Email Sent!
} + + Subject:
+
+ To(Email):
+
+ Msg:
+
+ + +
+} \ No newline at end of file diff --git a/app/page.tsx b/app/page.tsx index 95bcd54..a1167e5 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,3 +1,10 @@ +import EmailForm from "./components/EmailForm"; + export default function Page() { - return

Hello, Home page!

+ + return
+

Hello, Home page!

+ + +
} \ No newline at end of file diff --git a/components/email-template.tsx b/components/email-template.tsx index 85628e5..adb6716 100644 --- a/components/email-template.tsx +++ b/components/email-template.tsx @@ -2,13 +2,16 @@ import * as React from "react"; interface EmailTemplateProps { firstName: string; + msg: string; } export const EmailTemplate: React.FC> = ({ firstName, + msg }) => (

Welcome, {firstName}!

+

{msg}

); diff --git a/next-env.d.ts b/next-env.d.ts index 4f11a03..40c3d68 100644 --- a/next-env.d.ts +++ b/next-env.d.ts @@ -2,4 +2,4 @@ /// // NOTE: This file should not be edited -// see https://nextjs.org/docs/basic-features/typescript for more information. +// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information. diff --git a/next.config.js b/next.config.js index fef5fff..3f91196 100644 --- a/next.config.js +++ b/next.config.js @@ -1,5 +1,5 @@ function throwError(envVar) { - throw `Abort: You need to define ${envVar} in the .env file.` + throw `Abort: You need to define ${envVar} in the .env.example file and rename it to .env.` } if (!process.env.RESEND_API_KEY) return throwError('RESEND_API_KEY'); \ No newline at end of file