mailing: grouped emails
This commit is contained in:
2
app.yml
2
app.yml
@@ -38,7 +38,7 @@ wwMeta:
|
|||||||
|
|
||||||
mail:
|
mail:
|
||||||
enabled: false
|
enabled: false
|
||||||
frequency: 10000 # 10"
|
frequency: 3000 # 10"
|
||||||
host: smtp.sparkpostmail.com
|
host: smtp.sparkpostmail.com
|
||||||
port: 587
|
port: 587
|
||||||
auth: LOGIN
|
auth: LOGIN
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import * as nodemailer from 'nodemailer'
|
|||||||
import * as os from 'os'
|
import * as os from 'os'
|
||||||
|
|
||||||
const DEFAULT_FREQUENCY = 5 * 60 * 1000 // 5'
|
const DEFAULT_FREQUENCY = 5 * 60 * 1000 // 5'
|
||||||
const queue: EmailContent[] = []
|
const queue: MailContent[] = []
|
||||||
let conf: ConfMail|undefined
|
let conf: ConfMail|undefined
|
||||||
|
|
||||||
export function initConfMail(confMail: ConfMail) {
|
export function initConfMail(confMail: ConfMail) {
|
||||||
@@ -26,16 +26,52 @@ export async function queueEmail(subject: string, html: string, cc?: string) {
|
|||||||
console.log(`[mail] added 1 mail to queue`)
|
console.log(`[mail] added 1 mail to queue`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function consumeMessages(cc: string): MailContent[] {
|
||||||
|
const mails: MailContent[] = []
|
||||||
|
for (let i = 0; i < queue.length; i++) {
|
||||||
|
const mail = queue[i]
|
||||||
|
if (mail.cc === (cc || undefined)) {
|
||||||
|
mails.push(queue[i])
|
||||||
|
queue.splice(i, 1)
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mails
|
||||||
|
}
|
||||||
|
|
||||||
async function consumeQueue() {
|
async function consumeQueue() {
|
||||||
console.log(`[mail] consuming mailing queue: ${queue.length} messages to be sent`)
|
console.log(`[mail] consuming mailing queue: ${queue.length} messages to be sent`)
|
||||||
while (queue.length) {
|
while (queue.length) {
|
||||||
const mail = queue.shift()
|
if (queue.length > 1) {
|
||||||
if (mail) {
|
const ccs = queue.reduce((ccs, mail) => {
|
||||||
|
const cc = getGroup(mail)
|
||||||
|
if (ccs.indexOf(cc) === -1) {
|
||||||
|
ccs.push(cc)
|
||||||
|
}
|
||||||
|
return ccs
|
||||||
|
}, [] as string[])
|
||||||
|
console.log(`[mail] grouping messages into ${ccs.length} group(s)`)
|
||||||
|
for (const cc of ccs) {
|
||||||
|
const messages: MailContent[] = consumeMessages(cc)
|
||||||
|
const subject = `[dw] [${os.hostname}] ${messages.length} notifications`
|
||||||
|
let body = ''
|
||||||
|
for (const message of messages) {
|
||||||
|
body += `<h1>${message.subject}</h1>\n`
|
||||||
|
body += `<div>${message.body}</div>\n`
|
||||||
|
}
|
||||||
|
await sendEmail(subject, body, cc || undefined)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const mail = queue.shift() as MailContent
|
||||||
await sendEmail(mail.subject, mail.body, mail.cc)
|
await sendEmail(mail.subject, mail.body, mail.cc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getGroup(mail: MailContent) {
|
||||||
|
return mail.cc || ''
|
||||||
|
}
|
||||||
|
|
||||||
async function sendEmail(subject: string, html: string, cc?: string) {
|
async function sendEmail(subject: string, html: string, cc?: string) {
|
||||||
|
|
||||||
if (!conf) {
|
if (!conf) {
|
||||||
@@ -111,7 +147,7 @@ export const mail = {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EmailContent {
|
interface MailContent {
|
||||||
subject: string
|
subject: string
|
||||||
body: string
|
body: string
|
||||||
cc?: string
|
cc?: string
|
||||||
|
|||||||
Reference in New Issue
Block a user