mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.5 KiB
80 lines
2.5 KiB
2 years ago
|
import nodemailer from 'nodemailer'
|
||
|
import { get, has, kebabCase, set, template } from 'lodash-es'
|
||
|
import fs from 'node:fs/promises'
|
||
|
import path from 'node:path'
|
||
6 years ago
|
|
||
2 years ago
|
export default {
|
||
6 years ago
|
transport: null,
|
||
|
templates: {},
|
||
|
init() {
|
||
2 years ago
|
if (get(WIKI.config, 'mail.host', '').length > 2) {
|
||
6 years ago
|
let conf = {
|
||
|
host: WIKI.config.mail.host,
|
||
|
port: WIKI.config.mail.port,
|
||
2 years ago
|
name: WIKI.config.mail.name,
|
||
5 years ago
|
secure: WIKI.config.mail.secure,
|
||
|
tls: {
|
||
|
rejectUnauthorized: !(WIKI.config.mail.verifySSL === false)
|
||
|
}
|
||
6 years ago
|
}
|
||
2 years ago
|
if (get(WIKI.config, 'mail.user', '').length > 1) {
|
||
6 years ago
|
conf = {
|
||
|
...conf,
|
||
|
auth: {
|
||
|
user: WIKI.config.mail.user,
|
||
|
pass: WIKI.config.mail.pass
|
||
|
}
|
||
|
}
|
||
|
}
|
||
2 years ago
|
if (get(WIKI.config, 'mail.useDKIM', false)) {
|
||
6 years ago
|
conf = {
|
||
|
...conf,
|
||
|
dkim: {
|
||
|
domainName: WIKI.config.mail.dkimDomainName,
|
||
|
keySelector: WIKI.config.mail.dkimKeySelector,
|
||
|
privateKey: WIKI.config.mail.dkimPrivateKey
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
this.transport = nodemailer.createTransport(conf)
|
||
|
} else {
|
||
|
WIKI.logger.warn('Mail is not setup! Please set the configuration in the administration area!')
|
||
|
this.transport = null
|
||
|
}
|
||
|
return this
|
||
|
},
|
||
|
async send(opts) {
|
||
|
if (!this.transport) {
|
||
|
WIKI.logger.warn('Cannot send email because mail is not setup in the administration area!')
|
||
6 years ago
|
throw new WIKI.Error.MailNotConfigured()
|
||
6 years ago
|
}
|
||
|
await this.loadTemplate(opts.template)
|
||
|
return this.transport.sendMail({
|
||
3 years ago
|
headers: {
|
||
|
'x-mailer': 'Wiki.js'
|
||
|
},
|
||
6 years ago
|
from: `"${WIKI.config.mail.senderName}" <${WIKI.config.mail.senderEmail}>`,
|
||
6 years ago
|
to: opts.to,
|
||
|
subject: `${opts.subject} - ${WIKI.config.title}`,
|
||
|
text: opts.text,
|
||
2 years ago
|
html: get(this.templates, opts.template)({
|
||
4 years ago
|
logo: (WIKI.config.logoUrl.startsWith('http') ? '' : WIKI.config.host) + WIKI.config.logoUrl,
|
||
6 years ago
|
siteTitle: WIKI.config.title,
|
||
6 years ago
|
copyright: WIKI.config.company.length > 0 ? WIKI.config.company : 'Powered by Wiki.js',
|
||
6 years ago
|
...opts.data
|
||
|
})
|
||
|
})
|
||
|
},
|
||
|
async loadTemplate(key) {
|
||
2 years ago
|
if (has(this.templates, key)) { return }
|
||
|
const keyKebab = kebabCase(key)
|
||
6 years ago
|
try {
|
||
|
const rawTmpl = await fs.readFile(path.join(WIKI.SERVERPATH, `templates/${keyKebab}.html`), 'utf8')
|
||
2 years ago
|
set(this.templates, key, template(rawTmpl))
|
||
6 years ago
|
} catch (err) {
|
||
|
WIKI.logger.warn(err)
|
||
|
throw new WIKI.Error.MailTemplateFailed()
|
||
|
}
|
||
|
}
|
||
|
}
|