server/src/EmailService.ts

41 lines
1.3 KiB
TypeScript
Raw Normal View History

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-11 22:42:00 +00:00
import { resolveContent } from 'nodemailer/lib/shared'
2020-07-10 20:44:01 +00:00
export default class EmailService {
static getTransporter() {
if (process.env.SMTP_HOST == undefined || process.env.SMTP_PORT == undefined) {
throw new Error("Invalid SMTP config")
}
return nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: parseInt(process.env.SMTP_PORT),
secure: process.env.SMTP_SECURE == 'true',
auth: {
user: process.env.SMTP_USERNAME,
pass: process.env.SMTP_PASSWORD,
}
})
}
2020-07-11 22:42:00 +00:00
static send(to: string, subject: string, html: string) {
return new Promise(resolve => {
console.log('to', to)
console.log('subject', subject)
console.log('html', html)
// this.getTransporter().sendMail({
// from: '"Forum des associations - Espace Condorcet Centre Social" <ne-pas-repondre@espacecondorcet.org>',
// to,
// subject,
// text: htmlToText.fromString(html, { wordwrap: 130 }),
// html,
// }).then(info => {
// console.log("Message sent: %s", info.messageId);
// console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// resolve(info)
// }).catch(err => {
// console.error(err)
// })
})
}
2020-07-10 20:44:01 +00:00
}