-
Notifications
You must be signed in to change notification settings - Fork 7
/
mailer.js
52 lines (45 loc) · 1.04 KB
/
mailer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import nodemailer from 'nodemailer'
import sendmailTransport from 'nodemailer-sendmail-transport'
import { getJob } from '.'
/**
* Default logger options
* @type {Object}
*/
const defaults = {
transport: sendmailTransport(),
mail: {
from: 'Dispo <dispo@example.com>'
}
}
/**
* Mailer
*/
export default class Mailer {
/**
* Creates an instance of Mailer.
*
* @memberOf Mailer
* @param {Object} [config={}]
*/
constructor (config) {
this.config = Object.assign({}, defaults, config)
}
/**
* Initializes a nodemailer transport
*
* @memberOf Mailer
* @return {Promise<void>}
*/
init () {
this._mailer = nodemailer.createTransport(this.config.transport)
}
async sendMail (job, message) {
const { data: { notifyOnError, name }, id } = await getJob(job)
if (!notifyOnError) return
this._mailer.sendMail(Object.assign({}, this.config.mail, {
to: notifyOnError,
subject: `Job "${name}" (id ${id}) failed`,
text: `Job "${name}" (id ${id}) failed\n\n${message}`
}))
}
}