From 3e4ae8c1dfaaace2483ce0be32dab140748a2b1a Mon Sep 17 00:00:00 2001 From: Omer Sabic Date: Mon, 24 Jun 2024 10:34:30 +0200 Subject: [PATCH] contact email + route --- src/routes/blog.js | 22 +++++++++++++++++++++- src/utils/email.js | 21 +++++++++++++++++---- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/routes/blog.js b/src/routes/blog.js index a301309..4ba0939 100644 --- a/src/routes/blog.js +++ b/src/routes/blog.js @@ -2,7 +2,7 @@ import { and, desc, eq, not, or, sql } from "drizzle-orm"; import { db } from "../db/index.js"; import { authMiddlewareFn } from "../modules/middleware.js"; import { articles, articles as articlesTable, signups as signupsTable, sites, users } from "../db/schemas.js"; -import { sendFreebie } from "../utils/email.js"; +import { sendContactEmail, sendFreebie } from "../utils/email.js"; /** * @@ -170,5 +170,25 @@ export const blogRoutes = (fastify, _, done) => { }); }); + fastify.post("/contact", { + body: { + email: { + type: "string", + format: "email" + }, + subject: { + type: "string" + }, + content: { + type: "string" + }, + blog_id: { + type: "string" + } + } + }, async (req, reply) => { + + }); + done(); }; diff --git a/src/utils/email.js b/src/utils/email.js index 5051614..30ab92f 100644 --- a/src/utils/email.js +++ b/src/utils/email.js @@ -7,18 +7,31 @@ import { MailtrapClient } from "mailtrap"; const TOKEN = "a0cc97f4f856d6c16d70bb5984e32cef"; const ENDPOINT = "https://send.api.mailtrap.io/"; +const default_sender = { + email: "mailtrap@demomailtrap.com", + name: "Mailtrap Test", +}; + const client = new MailtrapClient({ endpoint: ENDPOINT, token: TOKEN }); +export async function sendContactEmail(recipient, sender, subject, body) { + client.send({ + from: default_sender, + to: { + email: recipient + }, + subject: "Form submission on YouPage.ai", + text: `You got a new message from your Youpage.ai page!\nSender: ${sender}\nSubject: ${subject}\nMessage: ${body}` + }).catch(console.error); +} + export async function sendFreebie(recipient, blog_id) { const [blog] = await db.select().from(sites).where(eq(sites.id, blog_id)); if (!blog) throw new Error("Invalid site"); if (!blog.use_freebie || !blog.freebie_url) return; - const sender = { - email: "mailtrap@demomailtrap.com", - name: "Mailtrap Test", - }; + const sender = default_sender; const recipients = [ {