gradido/backend/src/mailer/sendEMail.ts
2022-03-07 21:56:03 +01:00

34 lines
875 B
TypeScript

import { createTransport } from 'nodemailer'
import CONFIG from '@/config'
export const sendEMail = async (emailDef: {
to: string
subject: string
text: string
}): Promise<boolean> => {
if (!CONFIG.EMAIL) {
// eslint-disable-next-line no-console
console.log('Emails are disabled via config')
return false
}
const transporter = createTransport({
host: CONFIG.EMAIL_SMTP_URL,
port: Number(CONFIG.EMAIL_SMTP_PORT),
secure: false, // true for 465, false for other ports
requireTLS: true,
auth: {
user: CONFIG.EMAIL_USERNAME,
pass: CONFIG.EMAIL_PASSWORD,
},
})
const info = await transporter.sendMail({
...emailDef,
from: `Gradido (nicht antworten) <${CONFIG.EMAIL_SENDER}>`,
})
if (!info.messageId) {
throw new Error('error sending notification email, but transaction succeed')
}
return true
}