feat: remove defer (#243)

* working on moving the email logic to the backend

* Moved nodemailer to the backend

* uninstall defer

* updated relative import path in tsconfig

* working on the zod validation

* Merge branch 'main' into send-email

* fixed the zod import issue

* fixed the zod import issue

* updated the env example files

* docs: update README.md (#249)

* fix: handle catex error

* chore: remove fly.io github action

* Update README.md

Updated the Readme to reflect changes made to the emailing flow

---------

Co-authored-by: Rotimi Best <[email protected]>

* chore: wrapp up email sending

---------

Co-authored-by: Rotimi Best <[email protected]>
This commit is contained in:
Em_Dev
2024-04-12 04:57:19 +01:00
committed by GitHub
co-authored by Rotimi Best
parent 75ad73b97f
commit 3fe15e6633
27 changed files with 523 additions and 215 deletions
+59
View File
@@ -0,0 +1,59 @@
const express = require('express');
const zod = require('zod');
const { getTransporter } = require('../utils/getTransporter');
const { withEmailTemplate } = require('../utils/withEmailTemplate');
const router = express.Router();
router.post('/', async (req, res) => {
try {
const mySchema = zod.array(
zod.object({
from: zod.string().optional(),
to: zod.string(),
subject: zod.string(),
content: zod.string(),
isPersonalEmail: zod.boolean().optional(),
replyTo: zod.string().optional()
})
);
mySchema.parse(req.body);
const emailDataArray = req.body;
Promise.all(
emailDataArray.map(async (emailData) => {
try {
const { from, to, subject, content, isPersonalEmail, replyTo } = emailData;
const transporter = await getTransporter(isPersonalEmail);
if (!transporter) {
return;
}
const res = await transporter.sendMail({
from: from || '"Best from ClassroomIO" <[email protected]>',
to,
subject,
replyTo,
html: withEmailTemplate(content)
});
console.log('Email status', res);
} catch (error) {
console.error('Error sending email', error);
}
})
);
console.log('Email sent');
return res.json({ success: true });
} catch (error) {
console.error('Error sending emails:', error);
return res.status(500).json({ error: 'Internal Server Error' });
}
});
module.exports = router;
+37
View File
@@ -0,0 +1,37 @@
const nodemailer = require('nodemailer');
const { SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASSWORD, SMTP_USER_NOTIFY, SMTP_PASSWORD_NOTIFY } =
process.env;
const getTransporter = async (isPersonal = false) => {
if (
!SMTP_HOST ||
!SMTP_PORT ||
!SMTP_USER ||
!SMTP_PASSWORD ||
!SMTP_USER_NOTIFY ||
!SMTP_PASSWORD_NOTIFY
) {
console.error('SMTP configuration missing');
return null;
}
try {
const transporter = nodemailer.createTransport({
host: SMTP_HOST,
port: parseInt(SMTP_PORT || '465', 10),
secure: true,
auth: {
user: isPersonal ? SMTP_USER : SMTP_USER_NOTIFY,
pass: isPersonal ? SMTP_PASSWORD : SMTP_PASSWORD_NOTIFY
}
});
await transporter.verify();
return transporter;
} catch (error) {
console.error('Transporter error:', error);
return null;
}
};
module.exports = { getTransporter };
+220
View File
@@ -0,0 +1,220 @@
const withEmailTemplate = (content) =>
`<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, minimum-scale=1"
/>
<base target="_blank" />
<style>
body {
background-color: #fff;
font-family: "Poppins", "Helvetica Neue", "Segoe UI", Helvetica,
sans-serif;
font-size: 15px;
font-weight: 300;
line-height: 26px;
margin: 0;
color: #1e293b;
}
pre {
background: #f4f4f4;
padding: 2px;
}
hr {
border-top: 1px dashed #94a3b8;
margin-top: 1rem;
margin-bottom: 1rem;
}
table {
width: 100%;
}
table td {
padding: 5px;
}
.socialicons {
max-width: 200px;
margin-left: auto;
margin-right: auto;
margin-top: 27px;
}
.wrap {
background-color: #f1f5f9;
padding: 30px;
max-width: 525px;
margin: 0 auto;
border-radius: 12px;
}
.brandcolor {
color: #00c4b8;
}
.tooltip {
background-color: #f1f5f9;
padding: 1rem;
border-radius: 1rem;
color: #475569;
margin-top: 15px;
margin-bottom: 15px;
}
.tooltip a {
color: #1e293b;
}
.button {
margin-top:12px;
background: #0233BD;
border-radius: 8px;
text-decoration: none !important;
color: #fff !important;
font-weight: 500;
padding: 10px 30px;
display: inline-block;
font-size: 0.9em;
}
.button:hover {
background: #0233BD;
}
.footer {
text-align: center;
font-size: 12px;
color: #cbd5e1;
}
.footer a {
color: #cbd5e1;
margin-right: 5px;
}
.gutter {
padding: 30px;
text-align: center;
}
img {
max-width: 100%;
height: auto;
}
.gutter img {
max-width: 280px;
}
a {
color: #0233BD;
}
a:hover {
color: #0233BD;
}
h1,
h2,
h3,
h4 {
font-weight: 600;
}
@media screen and (max-width: 600px) {
.wrap {
max-width: auto;
}
.gutter {
padding: 10px;
}
}
</style>
</head>
<body
style="
background-color: #fff;
font-family: 'Poppins', 'Helvetica Neue', 'Segoe UI', Helvetica,
sans-serif;
font-size: 15px;
line-height: 26px;
margin: 0;
color: #1e293b;
"
>
<div class="gutter" style="padding: 30px">
<a href="https://classroomio.com" target="_blank">
<img
src="https://tapaozmyjsuykgerrfkt.supabase.co/storage/v1/object/public/avatars/classroomio-logo-full.png"
alt="ClassroomIO Logo"
/></a>
</div>
<div
class="wrap"
style="
background-color: #f1f5f9;
padding: 30px;
max-width: 525px;
margin: 0 auto;
border-radius: 12px;
"
>
${content}
</div>
<div
class="footer"
style="text-align: center; font-size: 12px; color: #cbd5e1"
>
<table class="socialicons">
<tr>
<td>
<a target="_blank" href="https://twitter.com/classroomio"
><img
title="X"
src="https://tapaozmyjsuykgerrfkt.supabase.co/storage/v1/object/public/avatars/classroomio-x.png"
alt="X"
width="32"
/></a>
</td>
<td>
<a target="_blank" href="https://www.youtube.com/@ClassroomIO"
><img
title="Youtube"
src="https://tapaozmyjsuykgerrfkt.supabase.co/storage/v1/object/public/avatars/classroomio-youtube.png"
alt="Youtube"
width="32"
/></a>
</td>
<td>
<a target="_blank" href="https://www.facebook.com/classroomiohq"
><img
title="Facebook"
src="https://tapaozmyjsuykgerrfkt.supabase.co/storage/v1/object/public/avatars/classroomio-facebook.png"
alt="Facebook"
width="32"
/></a>
</td>
</tr>
</table>
<p style="padding-top: 8px; line-height: initial">
ClassroomIO ${new Date().getFullYear()}. All rights reserved.<br />
<a
style="text-decoration: none"
href="https://app.enzuzo.com/policies/tos/958fc978-5477-11ee-a03b-7b111830c594"
target="_blank"
>Terms</a
>
|
<a
style="text-decoration: none"
href="https://app.enzuzo.com/policies/privacy/958fc978-5477-11ee-a03b-7b111830c594"
target="_blank"
>Privacy Policy</a
>
</p>
</div>
</body>
</html>
`;
module.exports = { withEmailTemplate };