-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
121 additions
and
64 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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 |
---|---|---|
@@ -1,48 +1,31 @@ | ||
import { NextResponse } from 'next/server'; | ||
import nodemailer from 'nodemailer'; | ||
import Mail from 'nodemailer/lib/mailer'; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { render } from "@react-email/render"; | ||
import Email from "@/components/devfest24-email"; | ||
import { handleSendEmail } from "@/lib/email"; | ||
|
||
type Payload = { | ||
to: string; | ||
subject: string; | ||
html: string; | ||
attachments: Mail.Attachment[]; | ||
}; | ||
|
||
export async function POST(request: Request) { | ||
export async function POST(request: NextRequest) { | ||
try { | ||
const data: Payload = await request.json(); | ||
const body = await request.json(); | ||
const { id, email, firstName, lastName } = body; | ||
|
||
let transporter = nodemailer.createTransport({ | ||
service: 'gmail', | ||
auth: { | ||
user: process.env.NODEMAILER_EMAIL, | ||
pass: process.env.NODEMAILER_PW, | ||
}, | ||
}); | ||
const htmlContent = await render(Email({ id, firstName, lastName })); | ||
|
||
await new Promise((resolve, reject) => { | ||
transporter.sendMail( | ||
{ | ||
from: process.env.NODEMAILER_EMAIL, | ||
...data, | ||
}, | ||
(error, info) => { | ||
if (error) { | ||
console.error('Error sending email:', error); | ||
reject(error); | ||
} else { | ||
console.log('Email sent:', info.response); | ||
resolve(info); | ||
} | ||
} | ||
); | ||
await handleSendEmail({ | ||
to: email, | ||
subject: "Certificate: Google DevFest 2024", | ||
html: htmlContent, | ||
}); | ||
|
||
return NextResponse.json({ message: 'Email sent successfully' }, { status: 200 }); | ||
} catch (error) { | ||
console.error('Error in route handler:', error); | ||
return NextResponse.json({ error: 'Failed to send email' }, { status: 500 }); | ||
return NextResponse.json({ message: "Success" }, { status: 200 }); | ||
} catch (err: any) { | ||
console.error("Error sending email:", err, process.env.NODEMAILER_PW); | ||
return NextResponse.json({ message: err.message }, { status: 500 }); | ||
} | ||
} | ||
|
||
export async function GET() { | ||
return NextResponse.json( | ||
{ message: "GET request received" }, | ||
{ status: 200 }, | ||
); | ||
} |
This file contains 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 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 |
---|---|---|
|
@@ -5,12 +5,18 @@ type Payload = { | |
to: string; | ||
subject: string; | ||
html: string; | ||
attachments: Mail.Attachment[]; | ||
attachments?: Mail.Attachment[]; | ||
}; | ||
|
||
export const handleSendEmail = async (data: Payload) => { | ||
let transporter = nodemailer.createTransport({ | ||
service: "gmail", | ||
host: "smtp.hostinger.com", | ||
port: 465, | ||
secure: true, | ||
debug: process.env.NODE_ENV === "development", | ||
tls: { | ||
rejectUnauthorized: false, | ||
}, | ||
auth: { | ||
user: process.env.NODEMAILER_EMAIL, | ||
pass: process.env.NODEMAILER_PW, | ||
|
@@ -19,11 +25,12 @@ export const handleSendEmail = async (data: Payload) => { | |
|
||
return ( | ||
await transporter.sendMail({ | ||
from: process.env.NODEMAILER_EMAIL, | ||
from: "[email protected]", | ||
...data, | ||
}), | ||
function (error: string, _info: string) { | ||
if (error) { | ||
console.log("Your Email", process.env.NODEMAILER_EMAIL); | ||
throw new Error(error); | ||
} else { | ||
console.log("Email Sent"); | ||
|