2020-07-11 22:42:00 +00:00
|
|
|
import htmlToText from 'html-to-text'
|
2020-07-10 20:44:01 +00:00
|
|
|
import nodemailer from 'nodemailer'
|
2020-07-12 10:30:55 +00:00
|
|
|
import fs from 'fs'
|
|
|
|
import twig from 'twig'
|
2020-07-10 20:44:01 +00:00
|
|
|
|
|
|
|
export default class EmailService {
|
2020-07-16 15:43:18 +00:00
|
|
|
static init() {
|
|
|
|
if (this.isMocked()) {
|
|
|
|
console.warn('> EmailService: WARNING: EMAIL IS MOCKED!')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-10 20:44:01 +00:00
|
|
|
static getTransporter() {
|
|
|
|
if (process.env.SMTP_HOST == undefined || process.env.SMTP_PORT == undefined) {
|
|
|
|
throw new Error("Invalid SMTP config")
|
|
|
|
}
|
2020-07-17 22:12:11 +00:00
|
|
|
let config: any = {
|
2020-07-10 20:44:01 +00:00
|
|
|
host: process.env.SMTP_HOST,
|
|
|
|
port: parseInt(process.env.SMTP_PORT),
|
2020-07-17 22:12:11 +00:00
|
|
|
secure: process.env.SMTP_SECURE == 'true'
|
|
|
|
}
|
|
|
|
if (process.env.SMTP_USERNAME !== undefined && process.env.SMTP_PASSWORD !== undefined) {
|
|
|
|
config.auth = {
|
2020-07-10 20:44:01 +00:00
|
|
|
user: process.env.SMTP_USERNAME,
|
|
|
|
pass: process.env.SMTP_PASSWORD,
|
|
|
|
}
|
2020-07-12 10:30:55 +00:00
|
|
|
}
|
2020-07-21 14:41:07 +00:00
|
|
|
//console.log(config)
|
2020-07-12 10:30:55 +00:00
|
|
|
return nodemailer.createTransport(config)
|
2020-07-10 20:44:01 +00:00
|
|
|
}
|
2020-07-11 22:42:00 +00:00
|
|
|
|
2020-07-14 21:58:55 +00:00
|
|
|
// static getMailgunClient(): Mailgun {
|
|
|
|
// return new Mailgun({
|
|
|
|
// apiKey: '4e64a894f18cc2fe10e4e4829b0048f3-7caa9475-241eee2c'
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
|
|
|
|
static nativeSend(params: any): Promise<any> {
|
|
|
|
// return this.getMailgunClient().sendMessage('sandboxeb2300b9439e4fe7bd2cca8f52ac7bd6.mailgun.org', {
|
|
|
|
// from: params.from,
|
|
|
|
// to: params.to,
|
|
|
|
// subject: params.subject,
|
|
|
|
// text: params.text,
|
|
|
|
// html: params.html
|
|
|
|
// })
|
|
|
|
return this.getTransporter().sendMail(params)
|
|
|
|
}
|
|
|
|
|
2020-07-12 10:30:55 +00:00
|
|
|
static send(to: string, subject: string, templateName: string, templateParams: any): Promise<any> {
|
|
|
|
const viewPath: string = __dirname + '/../views/emails/'
|
|
|
|
const data: Buffer = fs.readFileSync(viewPath + templateName + '.twig')
|
2020-07-16 15:43:18 +00:00
|
|
|
const html: string = twig.twig({
|
|
|
|
// @ts-ignore
|
|
|
|
allowInlineIncludes: true,
|
|
|
|
data: data.toString()
|
|
|
|
}).render(templateParams)
|
2020-07-12 10:30:55 +00:00
|
|
|
const text: string = htmlToText.fromString(html, { wordwrap: 130 })
|
|
|
|
|
|
|
|
// for now replace every email by a predefined one
|
2020-07-17 22:12:11 +00:00
|
|
|
// console.info(to + ' replaced')
|
|
|
|
// to = "spamfree@matthieubessat.fr"
|
2020-07-14 21:58:55 +00:00
|
|
|
subject += " - Forum des associations 2020"
|
2020-07-12 10:30:55 +00:00
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const config: any = {
|
|
|
|
from: '"Forum des associations - Espace Condorcet Centre Social" <ne-pas-repondre@espacecondorcet.org>',
|
|
|
|
to, subject, text, html
|
|
|
|
}
|
2020-07-16 15:43:18 +00:00
|
|
|
if (this.isMocked()) {
|
|
|
|
delete config.html
|
|
|
|
console.log(config)
|
2020-07-12 10:30:55 +00:00
|
|
|
resolve()
|
2020-07-16 15:43:18 +00:00
|
|
|
} else {
|
|
|
|
this.nativeSend(config).then(res => {
|
|
|
|
console.log('> A "' + templateName + '" email was sent')
|
2020-07-21 14:41:07 +00:00
|
|
|
resolve(res)
|
2020-07-16 15:43:18 +00:00
|
|
|
}).catch(err => {
|
|
|
|
console.log('> A "' + templateName + '" email failed to be sent')
|
|
|
|
console.error(err)
|
|
|
|
reject()
|
|
|
|
})
|
|
|
|
}
|
2020-07-11 22:42:00 +00:00
|
|
|
})
|
|
|
|
}
|
2020-07-16 15:43:18 +00:00
|
|
|
|
|
|
|
static getWebUiBaseUrl() {
|
2020-07-17 22:12:11 +00:00
|
|
|
return process.env.WEB_UI_URL === undefined ? "WEB_UI_URL_NOT_FOUND" : process.env.WEB_UI_URL
|
2020-07-16 15:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static getBaseUrl() {
|
2020-07-17 22:12:11 +00:00
|
|
|
return process.env.BASE_URL === undefined ? "BASE_URL_NOT_FOUND" : process.env.BASE_URL
|
2020-07-16 15:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static isMocked() {
|
|
|
|
// WARNING ATTENTION REQUIRED
|
|
|
|
// CUSTOM DESACTIVATION PLEASE ENABLE IN PRODUCTION
|
|
|
|
return process.env.MOCK_EMAIL === "true"
|
|
|
|
}
|
2020-07-17 22:12:11 +00:00
|
|
|
|
|
|
|
static getAdminAddress() {
|
|
|
|
return process.env.ADMIN_ADDRESS === undefined ? "ADMIN_ADDRESS_NOT_FOUND" : process.env.ADMIN_ADDRESS
|
|
|
|
}
|
2020-07-10 20:44:01 +00:00
|
|
|
}
|