| const fs = require('fs'); |
| const path = require('path'); |
| const nodemailer = require('nodemailer'); |
| const handlebars = require('handlebars'); |
| const { isEnabled } = require('~/server/utils/handleText'); |
| const logger = require('~/config/winston'); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const sendEmail = async ({ email, subject, payload, template, throwError = true }) => { |
| try { |
| const transporterOptions = { |
| |
| secure: process.env.EMAIL_ENCRYPTION === 'tls', |
| |
| requireTls: process.env.EMAIL_ENCRYPTION === 'starttls', |
| tls: { |
| |
| rejectUnauthorized: !isEnabled(process.env.EMAIL_ALLOW_SELFSIGNED), |
| }, |
| auth: { |
| user: process.env.EMAIL_USERNAME, |
| pass: process.env.EMAIL_PASSWORD, |
| }, |
| }; |
|
|
| if (process.env.EMAIL_ENCRYPTION_HOSTNAME) { |
| |
| transporterOptions.tls.servername = process.env.EMAIL_ENCRYPTION_HOSTNAME; |
| } |
|
|
| |
| if (process.env.EMAIL_SERVICE) { |
| transporterOptions.service = process.env.EMAIL_SERVICE; |
| } else { |
| transporterOptions.host = process.env.EMAIL_HOST; |
| transporterOptions.port = process.env.EMAIL_PORT ?? 25; |
| } |
|
|
| const transporter = nodemailer.createTransport(transporterOptions); |
|
|
| const source = fs.readFileSync(path.join(__dirname, 'emails', template), 'utf8'); |
| const compiledTemplate = handlebars.compile(source); |
| const options = () => { |
| return { |
| |
| from: |
| `"${process.env.EMAIL_FROM_NAME || process.env.APP_TITLE}"` + |
| `<${process.env.EMAIL_FROM}>`, |
| to: `"${payload.name}" <${email}>`, |
| envelope: { |
| |
| |
| from: process.env.EMAIL_FROM, |
| to: email, |
| }, |
| subject: subject, |
| html: compiledTemplate(payload), |
| }; |
| }; |
|
|
| |
| return await transporter.sendMail(options()); |
| } catch (error) { |
| if (throwError) { |
| throw error; |
| } |
| logger.error('[sendEmail]', error); |
| return error; |
| } |
| }; |
|
|
| module.exports = sendEmail; |
|
|