-
Notifications
You must be signed in to change notification settings - Fork 308
Expand file tree
/
Copy pathemail-service.js
More file actions
66 lines (54 loc) · 1.62 KB
/
Copy pathemail-service.js
File metadata and controls
66 lines (54 loc) · 1.62 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict'
const nodemailer = require('nodemailer')
const extend = require('extend')
const fs = require('fs')
const defaultWelcome = {
subject: 'Welcome to Solid {{name}}',
text: 'Your account has been created at the following webid: {{webid}}',
html: '<b>Your account has been created at the following webid: {{webid}}</b>'
}
const defaultMessage = {
subject: 'Message from {{me}}',
text: 'You have received a message from {{me}}:\n{{message}}',
html: '<p>You have received a message from<strong>{{me}}</strong></p><p>{{message}}</p>'
}
class EmailService {
constructor (settings = {}) {
// This reflects nodemailer string option, we allow it
if (typeof settings !== 'string') {
settings = extend(settings, {secure: true})
}
this.mailer = nodemailer.createTransport(settings)
if (settings.sender) {
this.sender = settings.sender
} else if (settings.host) {
this.sender = `no-reply@${settings.host}`
}
this.templates = settings.templates || {}
}
sendMail (email, callback) {
this.mailer.sendMail(email, callback)
}
readTemplate (path, defaultTemplate, cb) {
if (!path) {
return cb(defaultTemplate)
}
fs.readFile(path, function (err, data) {
if (err) return cb(defaultTemplate)
let json
try {
json = JSON.parse(data)
} catch (e) {
cb(defaultTemplate)
}
cb(json)
})
}
welcomeTemplate (cb) {
this.readTemplate(this.templates.welcomePath, defaultWelcome, cb)
}
messageTemplate (cb) {
this.readTemplate(this.templates.messatePath, defaultMessage, cb)
}
}
module.exports = EmailService