diff --git a/back/mails/EmailManager.ts b/back/mails/EmailManager.ts new file mode 100644 index 0000000..fc9fd07 --- /dev/null +++ b/back/mails/EmailManager.ts @@ -0,0 +1,44 @@ +import { ImapInstance } from "./imap/ImapInstance"; +import { SmtpInstance } from "./smtp/SmtpInstance"; +import logger from "../system/Logger"; +import { getAllAccounts } from "../db/imap/imap-db"; + +export interface Account { + id: number; + user: string + password?: string +} + +class EmailManager { + imapInstances: ImapInstance[] + smtpInstances: SmtpInstance[] + + constructor() { + this.imapInstances = []; + this.smtpInstances = []; + } + + init() { + getAllAccounts().then((accounts: Account[]) => { + for (let i = 0; i < accounts.length; i++) { + accounts[i].password = accounts[i]?.password?.toString().replace(/[\u{0080}-\u{FFFF}]/gu,""); + if (accounts[i].id == 2) continue; //debug_todo + this.addImapInstance(accounts[i]); + this.addSmtpInstance(accounts[i]); + } + }).catch((err) => { + logger.err(err); + }); + } + + addImapInstance(config) { + this.imapInstances.push(new ImapInstance(config)); + } + + addSmtpInstance(config) { + this.smtpInstances.push(new SmtpInstance(config)); + } +} + +const emailManager = new EmailManager(); +export default emailManager; \ No newline at end of file diff --git a/back/mails/imap/ImapInstance.ts b/back/mails/imap/ImapInstance.ts index 064307e..8afe950 100644 --- a/back/mails/imap/ImapInstance.ts +++ b/back/mails/imap/ImapInstance.ts @@ -1,4 +1,4 @@ -import { Account } from "./ImapSync"; +import { Account } from "../EmailManager"; import Imap from "imap"; import { getAllMailboxes, registerMailbox } from "../../db/imap/imap-db"; diff --git a/back/mails/imap/ImapSync.ts b/back/mails/imap/ImapSync.ts deleted file mode 100644 index e71b1d6..0000000 --- a/back/mails/imap/ImapSync.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { getAllAccounts } from "../../db/imap/imap-db"; -import logger from "../../system/Logger"; -import { ImapInstance } from "./ImapInstance"; - -export interface Account { - id: number; - user: string - password?: string -} - -export default class ImapSync { - instances: ImapInstance[] - - constructor() { - this.instances = []; - } - - init() { - getAllAccounts().then((accounts: Account[]) => { - for (let i = 0; i < accounts.length; i++) { - accounts[i].password = accounts[i]?.password?.toString().replace(/[\u{0080}-\u{FFFF}]/gu,""); - if (accounts[i].id == 2) continue; //debug_todo - this.addInstance(accounts[i]); - } - }).catch((err) => { - logger.err(err); - }); - } - - addInstance(config) { - this.instances.push(new ImapInstance(config)); - } -} \ No newline at end of file diff --git a/back/mails/smtp/SmtpInstance.ts b/back/mails/smtp/SmtpInstance.ts index 3406db0..0c8f378 100644 --- a/back/mails/smtp/SmtpInstance.ts +++ b/back/mails/smtp/SmtpInstance.ts @@ -1,12 +1,10 @@ -import { Account } from "../imap/ImapSync"; import logger from "../../system/Logger"; import nodemailer, { Transporter } from "nodemailer"; -export class SmapInstance { +export class SmtpInstance { transporter: Transporter; - account: Account; - constructor(account) { + constructor(account: {user: string, password: string}) { // todo store other data this.transporter = nodemailer.createTransport({ host: "smtp.gmail.com", @@ -17,7 +15,6 @@ export class SmapInstance { pass: account.password, }, }); - this.account = account; } sendMail() { diff --git a/back/server.ts b/back/server.ts index 51a2bd0..b1e1815 100644 --- a/back/server.ts +++ b/back/server.ts @@ -1,7 +1,7 @@ import express from "express"; import cors from "cors"; const app = express(); -import ImapSync from "./mails/imap/ImapSync"; +import ImapSync from "./mails/EmailManager"; import { execQueryAsync, execQuery } from "./db/db"; app.use(express.json()); @@ -14,12 +14,12 @@ app.use(cors()); app.listen(process.env.PORT || 5500); import mailRouter from "./routes/mail"; -import logger from "./system/Logger"; +import emailManager from "./mails/EmailManager"; app.use("/api/mail", mailRouter); -const imapSync = new ImapSync(); -imapSync.init(); +emailManager.init(); +// debug reset all tables const shouldReset = false; if (shouldReset) { const query = "SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE table_schema = 'mail'";