-
Notifications
You must be signed in to change notification settings - Fork 0
Mac 239 setup resend emails #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
a405019
feat(email): add email template and send API route
stevenphanny fc654b8
chore(MAC-239): small tailwind change
stevenphanny 1358792
feat(email): add email template and send API route
stevenphanny 392f614
chore(MAC-239): small tailwind change
stevenphanny 403e054
Merge branch 'mac-239-setup-resend-emails' of https://github.com/mona…
stevenphanny aa9a659
feat(MAC-239): setup basic form for testing and temporarily using ono…
stevenphanny f080e06
feat(MAC-239): created email-template for nicer gmail view
stevenphanny 2ac7c7c
fix(MAC-239): removed phone fields from form
stevenphanny 3ddd1c1
fix(MAC-239): custom error handling because default look ugly
stevenphanny 9840960
feat(MAC-239): setup basic form for testing and temporarily using ono…
stevenphanny f3dda9b
feat(MAC-239): created email-template for nicer gmail view
stevenphanny 1f7cdb3
fix(MAC-239): removed phone fields from form
stevenphanny 5b9a4bb
fix(MAC-239): custom error handling because default look ugly
stevenphanny 8323bd4
Merge branch 'mac-239-setup-resend-emails' of https://github.com/mona…
stevenphanny 6f31601
Merge remote-tracking branch 'origin' into mac-239-setup-resend-emails
stevenphanny edf5957
fix(MAC-239): fix colouring to match new colours
stevenphanny b908658
fix(MAC-239): .env.example was wrong
stevenphanny 37a08ec
Update app/api/send/route.ts
stevenphanny b01440b
Update package.json
stevenphanny 7bf9e03
Update components/ContactPageClient.tsx
stevenphanny ef48664
Update .env.example
stevenphanny e585590
Update components/ContactPageClient.tsx
stevenphanny e679c5f
fix(MAC-239): adding resend api key check and fixing css on contact page
stevenphanny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { EmailTemplate } from '../../../components/EmailTemplate'; | ||
| import { Resend } from 'resend'; | ||
|
|
||
| // Validate RESEND_API_KEY is configured | ||
| const apiKey = process.env.RESEND_API_KEY; | ||
| if (!apiKey || apiKey.trim().length === 0) { | ||
| throw new Error( | ||
| 'RESEND_API_KEY is not configured. Please set the RESEND_API_KEY environment variable.' | ||
| ); | ||
| } | ||
|
|
||
| const resend = new Resend(apiKey); | ||
|
|
||
| function isNonEmptyString(value: unknown): value is string { | ||
| return typeof value === 'string' && value.trim().length > 0; | ||
| } | ||
|
|
||
| function isValidEmail(value: unknown): value is string { | ||
| if (typeof value !== 'string') return false; | ||
| const email = value.trim(); | ||
| // Basic email pattern; not exhaustive but sufficient for simple validation | ||
| const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | ||
| return email.length > 0 && emailRegex.test(email); | ||
| } | ||
|
|
||
| export async function POST(req: Request) { | ||
| try { | ||
| // Get the form data from the request | ||
| const body = await req.json(); | ||
|
|
||
| if (!body || typeof body !== 'object') { | ||
| return Response.json( | ||
| { error: 'Invalid request body; expected JSON object.' }, | ||
| { status: 400 }, | ||
| ); | ||
| } | ||
|
|
||
| const { | ||
| name, | ||
| emailAddress, | ||
| subject, | ||
| message, | ||
| } = body as { | ||
| name?: unknown; | ||
| emailAddress?: unknown; | ||
| subject?: unknown; | ||
| message?: unknown; | ||
| }; | ||
|
|
||
| if (!isNonEmptyString(name)) { | ||
| return Response.json( | ||
| { error: 'Field "name" is required and must be a non-empty string.' }, | ||
| { status: 400 }, | ||
| ); | ||
| } | ||
|
|
||
| if (!isValidEmail(emailAddress)) { | ||
| return Response.json( | ||
| { error: 'Field "emailAddress" is required and must be a valid email address.' }, | ||
| { status: 400 }, | ||
| ); | ||
| } | ||
|
|
||
| if (!isNonEmptyString(message)) { | ||
| return Response.json( | ||
| { error: 'Field "message" is required and must be a non-empty string.' }, | ||
| { status: 400 }, | ||
| ); | ||
| } | ||
|
|
||
| const normalizedSubject = | ||
| typeof subject === 'string' && subject.trim().length > 0 | ||
| ? subject | ||
| : 'New Message from Monash Coding Site'; | ||
|
|
||
| const { data, error } = await resend.emails.send({ | ||
| from: 'noreply@monashcoding.com', | ||
| // to: 'coding@monashclubs.org', | ||
| to: 'projects@monashcoding.com', | ||
| replyTo: (emailAddress as string).trim(), // User's email will be set as reply-to | ||
| subject: normalizedSubject, | ||
| react: EmailTemplate({ | ||
| name: (name as string).trim(), | ||
| emailAddress: (emailAddress as string).trim(), | ||
| subject: normalizedSubject, | ||
| message: (message as string).trim(), | ||
| }), | ||
| }); | ||
|
|
||
| if (error) { | ||
| console.error('Resend API error:', error); | ||
| return Response.json({ error }, { status: 500 }); | ||
| } | ||
|
|
||
| return Response.json(data); | ||
| } catch (error) { | ||
| console.error('Catch error:', error); | ||
| return Response.json({ error: String(error) }, { status: 500 }); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.