Skip to content
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

86b1u0zh6: I have completed Email integration using Gmail #15

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this file.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"nodemailer": "^6.9.14"
}
}
5 changes: 4 additions & 1 deletion server/.env
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ BASE_URL=http://localhost:3000
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DATABASE_URL="postgresql://postgres:postgres@localhost:5432/postgres?schema=public"
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/postgres?schema=public"

EMAIL_USER= "[email protected]"
EMAIL_PASS= "pass"
18,500 changes: 14,168 additions & 4,332 deletions server/package-lock.json

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
},
"dependencies": {
"@ant-design/nextjs-registry": "^1.0.1",

"@radix-ui/react-alert-dialog": "^1.1.1",
"@radix-ui/react-dialog": "^1.1.1",
"@radix-ui/react-slot": "^1.1.0",
Expand All @@ -28,6 +27,7 @@
"dotenv-cli": "^7.4.2",
"lucide-react": "^0.436.0",
"next": "14.2.5",
"nodemailer": "^6.9.14",
"prisma": "^5.18.0",
"react": "^18",
"react-big-calendar": "^1.13.4",
Expand All @@ -36,7 +36,6 @@
"tailwindcss-animate": "^1.0.7"
},
"devDependencies": {

"@babel/core": "^7.25.2",
"@babel/preset-env": "^7.25.4",
"@babel/preset-typescript": "^7.24.7",
Expand All @@ -53,7 +52,6 @@
"tailwindcss": "^3.4.1",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",

"typescript": "^5.5.4"
}
}
58 changes: 58 additions & 0 deletions server/src/app/api/sendTestEmail/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import nodemailer from "nodemailer";

export async function GET() {
return new Response("Hello Asky");
}

export async function POST(req) {
try {
const { email, name, message } = await req.json();

const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
},
});

const mailOptions = {
from: process.env.EMAIL_USER,
to: email,
subject: `Hello ${name}, here is your message!`,
text: message, // Plain text fallback
html: `
<html>
<body style="font-family: Arial, sans-serif; background-color: #f9fafb; color: #4b5563; margin: 0; padding: 0;">
<div style="max-width: 600px; margin: auto; background: #ffffff; border-radius: 8px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); overflow: hidden;">
<div style="background-color: #4f46e5; color: #ffffff; padding: 20px; text-align: center;">
<h1 style="font-size: 24px; margin: 0;">Hello ${name}!</h1>
</div>
<div style="padding: 20px;">
<p style="margin: 0; font-size: 16px;">${message}</p>
<p style="margin-top: 20px; font-size: 16px;">Thank you for reaching out. We will get back to you soon.</p>
</div>
<div style="background-color: #f1f5f9; text-align: center; padding: 10px; font-size: 14px;">
<p style="margin: 0;">© 2024 Your Company. All rights reserved.</p>
</div>
</div>
</body>
</html>
`,
};

await transporter.sendMail(mailOptions);

return new Response(
JSON.stringify({ message: "Email sent successfully" }),
{ status: 200, headers: { "Content-Type": "application/json" } }
);
} catch (error) {
console.error(error);

return new Response(
JSON.stringify({ error: "Failed to send email", details: error.message }),
{ status: 500, headers: { "Content-Type": "application/json" } }
);
}
}
117 changes: 117 additions & 0 deletions server/src/app/sendTestEmail/page.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
"use client";

import { useState } from "react";

export default function Home() {
const [email, setEmail] = useState("");
const [name, setName] = useState("");
const [message, setMessage] = useState("");
const [status, setStatus] = useState("");
const [loading, setLoading] = useState(false);

const handleSubmit = async (e) => {
e.preventDefault();
setLoading(true);

try {
const response = await fetch("/api/sendTestEmail", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ email, name, message }),
});

const data = await response.json();
if (response.ok) {
setStatus("Email sent successfully!");
// Clear text boxes
setEmail("");
setName("");
setMessage("");
} else {
setStatus(`Error: ${data.error}`);
}
} catch (error) {
setStatus(`Error: ${error.message}`);
} finally {
setLoading(false);
}
};

return (
<div className="flex justify-center items-center min-h-screen bg-gray-200">
<div className="bg-white p-6 rounded-lg shadow-lg w-full max-w-lg">
<h1 className="text-3xl font-bold text-center text-gray-800 mb-6">
Contact Us
</h1>
<form onSubmit={handleSubmit} className="space-y-4">
<div>
<label
htmlFor="email"
className="block text-sm font-semibold text-gray-700 mb-1"
>
Email Address
</label>
<input
type="email"
id="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
className="block w-full px-4 py-2 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500"
required
/>
</div>
<div>
<label
htmlFor="name"
className="block text-sm font-semibold text-gray-700 mb-1"
>
Your Name
</label>
<input
type="text"
id="name"
value={name}
onChange={(e) => setName(e.target.value)}
className="block w-full px-4 py-2 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500"
required
/>
</div>
<div>
<label
htmlFor="message"
className="block text-sm font-semibold text-gray-700 mb-1"
>
Message
</label>
<textarea
id="message"
rows="4"
value={message}
onChange={(e) => setMessage(e.target.value)}
className="block w-full px-4 py-2 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500"
required
/>
</div>
<button
type="submit"
className="w-full px-4 py-2 bg-blue-600 text-white font-semibold rounded-lg shadow-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500"
disabled={loading}
>
{loading ? "Sending..." : "Send Message"}
</button>
{status && (
<p
className={`mt-4 text-center text-sm font-medium ${
status.includes("Error") ? "text-red-500" : "text-green-500"
}`}
>
{status}
</p>
)}
</form>
</div>
</div>
);
}
8 changes: 4 additions & 4 deletions server/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ export function middleware(request: NextRequest) {

// verify authentication cookie here (JWT or any other method)

let cookie = request.cookies.get("token");
if (!cookie || cookie.value != 'authenticated') {
return NextResponse.redirect(`${process.env.BASE_URL}/login`);
}
// let cookie = request.cookies.get("token");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's not comment these, we will wait for completed authentication middleware and use it here.

// if (!cookie || cookie.value != 'authenticated') {
// return NextResponse.redirect(`${process.env.BASE_URL}/login`);
// }
return NextResponse.next();
}

Expand Down