add working send email
This commit is contained in:
parent
46fdd77353
commit
7cd5113ef0
2036
package-lock.json
generated
2036
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -16,16 +16,23 @@
|
||||
"axios": "^1.13.2",
|
||||
"clsx": "^2.1.1",
|
||||
"date-fns": "^4.1.0",
|
||||
"dompurify": "^3.3.1",
|
||||
"ioredis": "^5.8.2",
|
||||
"jsdom": "^27.4.0",
|
||||
"next": "^14.2.35",
|
||||
"nodemailer": "^7.0.12",
|
||||
"react": "^18",
|
||||
"react-dom": "^18"
|
||||
"react-dom": "^18",
|
||||
"validator": "^13.15.26"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@iconify/react": "^5.2.0",
|
||||
"@types/jsdom": "^27.0.0",
|
||||
"@types/node": "^20",
|
||||
"@types/nodemailer": "^7.0.4",
|
||||
"@types/react": "^18",
|
||||
"@types/react-dom": "^18",
|
||||
"@types/validator": "^13.15.10",
|
||||
"add": "^2.0.6",
|
||||
"eslint-config-next": "^16.0.10",
|
||||
"postcss": "^8",
|
||||
|
||||
@ -1,5 +1,9 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { rateLimited } from "@/lib/server-utils";
|
||||
|
||||
import { rateLimited, sanitize, sendEmail } from "@/lib/server-utils";
|
||||
import { trimTooLong } from "@/lib/strings";
|
||||
|
||||
import validator from "validator";
|
||||
|
||||
const validateInput = (data: any) => {
|
||||
return (
|
||||
@ -15,7 +19,8 @@ const validateInput = (data: any) => {
|
||||
!data.anon &&
|
||||
(
|
||||
!data.name.trim() ||
|
||||
!data.email.trim()
|
||||
!data.email.trim() ||
|
||||
!validator.isEmail(data.email)
|
||||
) ||
|
||||
!data.message.trim()
|
||||
)
|
||||
@ -50,5 +55,20 @@ export async function POST(req: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const name = trimTooLong(data.name as string, 20);
|
||||
const rawMessage = trimTooLong(data.message, 5000);
|
||||
const message = sanitize(validator.escape(rawMessage));
|
||||
|
||||
await sendEmail(name, data.email, message);
|
||||
|
||||
return NextResponse.json({ status: "ok" });
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
return NextResponse.json({
|
||||
status: "failed",
|
||||
message: err instanceof Error ? err.message : ''
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
'use client'
|
||||
|
||||
import { useRef, useState } from "react";
|
||||
import axios from "axios";
|
||||
import axios, { AxiosError } from "axios";
|
||||
|
||||
import { CheckboxInput, Input, Submit } from "./form";
|
||||
import { FloatingLabel } from "../floating-label";
|
||||
@ -54,9 +54,12 @@ export default function ContactForm() {
|
||||
setStatus('success');
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
setStatus('failed');
|
||||
setErrorMsg("Something went wrong, Sorry...");
|
||||
|
||||
if (err instanceof AxiosError) {
|
||||
if (err.status == 429) setErrorMsg("Limit reached, please try again later...");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -98,7 +101,7 @@ export default function ContactForm() {
|
||||
/>
|
||||
</label>
|
||||
<div className="text-primary">{errorMsg}</div>
|
||||
<Submit className="w-full">{statusMsg[status]}</Submit>
|
||||
<Submit disabled={status === "loading"} className="w-full">{statusMsg[status]}</Submit>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
12
src/lib/mailer.ts
Normal file
12
src/lib/mailer.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import nodemailer from "nodemailer";
|
||||
|
||||
export const transporter = nodemailer.createTransport({
|
||||
// @ts-ignore
|
||||
host: process.env.SMTP_HOST,
|
||||
port: Number(process.env.SMTP_PORT),
|
||||
secure: process.env.SMTP_SECURE,
|
||||
auth: {
|
||||
user: process.env.SMTP_USER,
|
||||
pass: process.env.SMTP_PASS
|
||||
}
|
||||
});
|
||||
@ -1,5 +1,9 @@
|
||||
import axios from "axios";
|
||||
import { JSDOM } from 'jsdom';
|
||||
import DOMPurify from "dompurify";
|
||||
|
||||
import { redis } from "./redis";
|
||||
import { transporter } from "./mailer";
|
||||
|
||||
export async function rateLimited(clientId: string) {
|
||||
const key = `contact:${clientId}`;
|
||||
@ -7,7 +11,7 @@ export async function rateLimited(clientId: string) {
|
||||
if (count === 1) {
|
||||
await redis.expire(key, 600);
|
||||
}
|
||||
return count > 5;
|
||||
return count > 3;
|
||||
}
|
||||
|
||||
export async function validateTurnstile(token: string, remoteip: string) {
|
||||
@ -32,6 +36,17 @@ export async function validateTurnstile(token: string, remoteip: string) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function sendEmail(target: string) {
|
||||
|
||||
export async function sendEmail(name: string, email: string, message: string) {
|
||||
await transporter.sendMail({
|
||||
from: `Nonszy Contact Form <${process.env.SMTP_USER}>`,
|
||||
replyTo: email,
|
||||
to: process.env.SMTP_REPLY,
|
||||
subject: `Message from ${name}`,
|
||||
text: message
|
||||
})
|
||||
}
|
||||
|
||||
export function sanitize(dirty: string) {
|
||||
const window = new JSDOM('').window;
|
||||
return DOMPurify(window).sanitize(dirty);
|
||||
}
|
||||
3
src/lib/strings.ts
Normal file
3
src/lib/strings.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export const trimTooLong = (text: string, limit: number, endsWith?: string) => {
|
||||
return text.length > limit ? text.slice(0, limit) + (endsWith ?? "...") : text;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user