CLASSROOMIO-T-213: Replace all email notifications with Nodemailer
This commit is contained in:
@@ -1,11 +1,24 @@
|
||||
import { supabase } from '$lib/utils/functions/supabase';
|
||||
|
||||
export const NOTIFICATION_NAME = {
|
||||
WELCOME_TO_APP: 'WELCOME TO APP'
|
||||
WELCOME_TO_APP: 'WELCOME TO APP',
|
||||
INVITE_TEACHER: 'INVITE TEACHER',
|
||||
WELCOME_TEACHER_TO_COURSE: 'WELCOME TEACHER TO COURSE',
|
||||
SEND_TEACHER_STUDENT_BUY_REQUEST: 'SEND TEACHER STUDENT BUY REQUEST',
|
||||
STUDENT_PROOVE_COURSE_PAYMENT: 'STUDENT PROOVE COURSE PAYMENT',
|
||||
STUDENT_COURSE_WELCOME: 'STUDENT COURSE WELCOME',
|
||||
TEACHER_STUDENT_JOINED: 'TEACHER STUDENT JOINED'
|
||||
};
|
||||
|
||||
const NAME_TO_PATH = {
|
||||
[NOTIFICATION_NAME.WELCOME_TO_APP]: '/api/email/welcome'
|
||||
[NOTIFICATION_NAME.WELCOME_TO_APP]: '/api/email/welcome',
|
||||
[NOTIFICATION_NAME.INVITE_TEACHER]: '/api/email/invite',
|
||||
[NOTIFICATION_NAME.WELCOME_TEACHER_TO_COURSE]: '/api/email/course/teacher_welcome',
|
||||
[NOTIFICATION_NAME.SEND_TEACHER_STUDENT_BUY_REQUEST]:
|
||||
'/api/email/course/teacher_student_buycourse',
|
||||
[NOTIFICATION_NAME.STUDENT_PROOVE_COURSE_PAYMENT]: '/api/email/course/student_prove_payment',
|
||||
[NOTIFICATION_NAME.STUDENT_COURSE_WELCOME]: '/api/email/course/student_welcome',
|
||||
[NOTIFICATION_NAME.TEACHER_STUDENT_JOINED]: '/api/email/course/teacher_student_joined'
|
||||
};
|
||||
|
||||
const getAccessToken = async () => {
|
||||
@@ -14,7 +27,7 @@ const getAccessToken = async () => {
|
||||
return data.session?.access_token || '';
|
||||
};
|
||||
|
||||
export const triggerSendEmail = async (name: string, body: { [k: string]: string }) => {
|
||||
export const triggerSendEmail = async (name: string, body: { [k: string]: any }) => {
|
||||
try {
|
||||
const accessToken = await getAccessToken();
|
||||
const path = NAME_TO_PATH[name];
|
||||
@@ -37,8 +50,8 @@ export const triggerSendEmail = async (name: string, body: { [k: string]: string
|
||||
}
|
||||
const result = await response.json();
|
||||
|
||||
console.log(`${name} sent email result`, result);
|
||||
console.log(name, result);
|
||||
} catch (error) {
|
||||
console.log(`${name} sent email error`, error);
|
||||
console.error(name, error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
import nodemailer from 'nodemailer';
|
||||
import { withEmailTemplate } from './template';
|
||||
import { SMTP_HOST, SMTP_USER, SMTP_PASSWORD } from '$env/static/private';
|
||||
import {
|
||||
SMTP_HOST,
|
||||
SMTP_USER,
|
||||
SMTP_PASSWORD,
|
||||
SMTP_USER_HELP,
|
||||
SMTP_PASSWORD_HELP
|
||||
} from '$env/static/private';
|
||||
|
||||
const content = `
|
||||
<div>
|
||||
@@ -13,18 +19,20 @@ Your ClassroomIO Team
|
||||
</div>
|
||||
`;
|
||||
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: SMTP_HOST,
|
||||
port: 465,
|
||||
secure: true,
|
||||
auth: {
|
||||
user: SMTP_USER,
|
||||
pass: SMTP_PASSWORD
|
||||
}
|
||||
});
|
||||
let transporter: nodemailer.Transporter;
|
||||
|
||||
const getTransporter = async (): Promise<boolean> => {
|
||||
const getTransporter = async (isPersonal?: boolean): Promise<boolean> => {
|
||||
return await new Promise((resolve, reject) => {
|
||||
transporter = nodemailer.createTransport({
|
||||
host: SMTP_HOST,
|
||||
port: 465,
|
||||
secure: true,
|
||||
auth: {
|
||||
user: isPersonal ? SMTP_USER : SMTP_USER_HELP,
|
||||
pass: isPersonal ? SMTP_PASSWORD : SMTP_PASSWORD_HELP
|
||||
}
|
||||
});
|
||||
|
||||
transporter.verify(function (error, success) {
|
||||
if (error) {
|
||||
console.log('Transporter error', error);
|
||||
@@ -38,19 +46,26 @@ const getTransporter = async (): Promise<boolean> => {
|
||||
};
|
||||
|
||||
export const sendEmail = async ({
|
||||
from,
|
||||
to,
|
||||
subject,
|
||||
content
|
||||
content,
|
||||
isPersonalEmail,
|
||||
replyTo
|
||||
}: {
|
||||
from?: string;
|
||||
to: string;
|
||||
subject: string;
|
||||
content: string;
|
||||
isPersonalEmail?: boolean;
|
||||
replyTo?: string;
|
||||
}) => {
|
||||
await getTransporter();
|
||||
await getTransporter(isPersonalEmail);
|
||||
const info = await transporter.sendMail({
|
||||
from: '"Best from ClassroomIO" <[email protected]>', // sender address
|
||||
from: from || '"Best from ClassroomIO" <[email protected]>', // sender address
|
||||
to, // list of receivers
|
||||
subject, // Subject line
|
||||
replyTo,
|
||||
html: withEmailTemplate(content) // html body
|
||||
});
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ export const withEmailTemplate = (content: string) =>
|
||||
|
||||
.button {
|
||||
margin-top:12px;
|
||||
background: #0f172a;
|
||||
background: #0233BD;
|
||||
border-radius: 8px;
|
||||
text-decoration: none !important;
|
||||
color: #fff !important;
|
||||
@@ -110,10 +110,10 @@ export const withEmailTemplate = (content: string) =>
|
||||
}
|
||||
|
||||
a {
|
||||
color: #00c4b8;
|
||||
color: #0233BD;
|
||||
}
|
||||
a:hover {
|
||||
color: #00e6ca;
|
||||
color: #0233BD;
|
||||
}
|
||||
h1,
|
||||
h2,
|
||||
|
||||
Reference in New Issue
Block a user