Skip to content

Commit

Permalink
fix: emails not sending - fix #41 (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
pagoru authored Sep 13, 2024
1 parent c3cce75 commit 960587e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 22 deletions.
2 changes: 1 addition & 1 deletion app/server/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
"yaml": "npm:[email protected]",

"bcrypt": "https://deno.land/x/[email protected]/mod.ts",
"smtp": "https://deno.land/x/[email protected]/mod.ts"
"nodemailer": "npm:[email protected]"
}
}
6 changes: 6 additions & 0 deletions app/server/deno.lock

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

1 change: 0 additions & 1 deletion app/server/src/modules/api/v2/account/login.request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export const loginRequest: RequestType = {
email,
]);

console.log(!accountByEmail);
if (!accountByEmail)
return Response.json(
{ status: 403 },
Expand Down
68 changes: 48 additions & 20 deletions app/server/src/system/email.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
import { SmtpClient } from "smtp";
import { System } from "./main.ts";
import nodemailer from "nodemailer";

export const email = () => {
const $connect = async () => {
const client = new SmtpClient();
const {
email: { hostname, port, username, password },
} = System.getConfig();
await client.connect({
hostname,
port,
username,
password,
let transporter;
const $loadTransporter = (currentResolve?: () => void) =>
new Promise<void>((resolve, reject) => {
const {
email: { hostname, port, username, password },
} = System.getConfig();

transporter = nodemailer.createTransport({
host: hostname,
port,
secure: true, // use TLS
auth: {
user: username,
pass: password,
},
tls: {
// do not fail on invalid certs
rejectUnauthorized: false,
},
});
const targetResolve = currentResolve ?? resolve;
transporter.verify((error, success) => {
if (error) {
console.error("Error loading email transporter!");
$loadTransporter(targetResolve);
return;
}
console.error("Email transporter loaded!");
targetResolve();
});
});
return client;

const load = async () => {
await $loadTransporter();
};

const send = async (
Expand All @@ -26,18 +48,23 @@ export const email = () => {
const $send = async () => {
const hiddenEmail = `${to.substring(0, 3)}***${to.substring(to.length - 3, to.length)}`;
try {
const client = await $connect();
const {
email: { username },
} = System.getConfig();
await client.send({
from: username,
to,
subject,
content,
html,
await new Promise<void>((resolve, reject) => {
transporter.sendMail(
{
from: username,
to,
subject: subject,
text: content,
html,
},
(error, info) => {
error ? reject() : resolve();
},
);
});
await client.close();
resolve();
console.log(`Email sent to ${hiddenEmail}!`);
} catch (e) {
Expand All @@ -49,6 +76,7 @@ export const email = () => {
});

return {
load,
send,
};
};
1 change: 1 addition & 0 deletions app/server/src/system/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const System = (() => {
$envs = envs;

await $db.load();
await $email.load();
$api.load();
};

Expand Down

0 comments on commit 960587e

Please sign in to comment.