start on repond to message (basic input and builder for dm)
This commit is contained in:
@@ -5,30 +5,32 @@ import { getAllAccounts } from "../db/imap/imap-db";
|
||||
|
||||
export interface Account {
|
||||
id: number;
|
||||
user: string
|
||||
password?: string
|
||||
user: string;
|
||||
password?: string;
|
||||
}
|
||||
|
||||
class EmailManager {
|
||||
imapInstances: ImapInstance[]
|
||||
smtpInstances: SmtpInstance[]
|
||||
|
||||
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);
|
||||
});
|
||||
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) {
|
||||
@@ -38,7 +40,11 @@ class EmailManager {
|
||||
addSmtpInstance(config) {
|
||||
this.smtpInstances.push(new SmtpInstance(config));
|
||||
}
|
||||
|
||||
getSmtp(email: string): SmtpInstance | undefined {
|
||||
return this.smtpInstances.find((instance) => instance.user == email);
|
||||
}
|
||||
}
|
||||
|
||||
const emailManager = new EmailManager();
|
||||
export default emailManager;
|
||||
export default emailManager;
|
||||
|
||||
@@ -3,9 +3,11 @@ import nodemailer, { Transporter } from "nodemailer";
|
||||
|
||||
export class SmtpInstance {
|
||||
transporter: Transporter;
|
||||
user: string;
|
||||
|
||||
constructor(account: {user: string, password: string}) {
|
||||
constructor(account: { user: string; password: string }) {
|
||||
// todo store other data
|
||||
this.user = account.user;
|
||||
this.transporter = nodemailer.createTransport({
|
||||
host: "smtp.gmail.com",
|
||||
port: 465,
|
||||
@@ -17,20 +19,22 @@ export class SmtpInstance {
|
||||
});
|
||||
}
|
||||
|
||||
sendMail() {
|
||||
const msg = {
|
||||
from: "",
|
||||
to: "",
|
||||
subject: "Hello ✔",
|
||||
text: "Hello world?",
|
||||
html: "<b>Hello world?</b>",
|
||||
};
|
||||
this.transporter.sendMail(msg, (err, message) => {
|
||||
if (err) {
|
||||
logger.err(err);
|
||||
throw err;
|
||||
}
|
||||
logger.log(message);
|
||||
});
|
||||
sendMail(message: any) {
|
||||
console.log(this.user)
|
||||
console.log(message)
|
||||
// const msg = {
|
||||
// from: "",
|
||||
// to: "",
|
||||
// subject: "Hello ✔",
|
||||
// text: "Hello world?",
|
||||
// html: "<b>Hello world?</b>",
|
||||
// };
|
||||
// this.transporter.sendMail(msg, (err, message) => {
|
||||
// if (err) {
|
||||
// logger.err(err);
|
||||
// throw err;
|
||||
// }
|
||||
// logger.log(message);
|
||||
// });
|
||||
}
|
||||
}
|
||||
|
||||
46
back/mails/utils/mailBuilder.ts
Normal file
46
back/mails/utils/mailBuilder.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
export default class MailBuilder {
|
||||
message: any;
|
||||
constructor(message = {}) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
from(addresses: string[] | string): MailBuilder {
|
||||
this.message.from = addresses;
|
||||
return this;
|
||||
}
|
||||
|
||||
to(addresses: string[] | string): MailBuilder {
|
||||
this.message.to = addresses;
|
||||
return this;
|
||||
}
|
||||
|
||||
cc(addresses: string[] | string): MailBuilder {
|
||||
this.message.cc = addresses;
|
||||
return this;
|
||||
}
|
||||
|
||||
bcc(addresses: string[] | string): MailBuilder {
|
||||
this.message.bcc = addresses;
|
||||
return this;
|
||||
}
|
||||
|
||||
subject(subject: string): MailBuilder {
|
||||
this.message.subject = subject;
|
||||
return this;
|
||||
}
|
||||
|
||||
text(textContent: string): MailBuilder {
|
||||
this.message.text = textContent;
|
||||
return this;
|
||||
}
|
||||
|
||||
html(htmlContent: string): MailBuilder {
|
||||
this.message.html = htmlContent;
|
||||
return this;
|
||||
}
|
||||
|
||||
inReplyTo(messageID: string): MailBuilder {
|
||||
this.message.inReplyTo = messageID;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user