mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
refactor email middleware
This commit is contained in:
parent
96ced53843
commit
ec0cf36256
@ -4,42 +4,38 @@ import { htmlToText } from 'nodemailer-html-to-text'
|
||||
import { resetPasswordMail, wrongAccountMail } from './templates/passwordReset'
|
||||
import { signupTemplate } from './templates/signup'
|
||||
|
||||
let sendMail
|
||||
if (CONFIG.SMTP_HOST && CONFIG.SMTP_PORT) {
|
||||
const hasEmailConfig = CONFIG.SMTP_HOST && CONFIG.SMTP_PORT
|
||||
const hasAuthData = CONFIG.SMTP_USERNAME && CONFIG.SMTP_PASSWORD
|
||||
|
||||
let sendMail = () => {}
|
||||
if (!hasEmailConfig) {
|
||||
if (process.env.NODE_ENV !== 'test') {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('Warning: Email middleware will not try to send mails.')
|
||||
}
|
||||
} else {
|
||||
sendMail = async templateArgs => {
|
||||
const transport = transporter()
|
||||
transport.use(
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: CONFIG.SMTP_HOST,
|
||||
port: CONFIG.SMTP_PORT,
|
||||
ignoreTLS: CONFIG.SMTP_IGNORE_TLS,
|
||||
secure: false, // true for 465, false for other ports
|
||||
auth: hasAuthData && {
|
||||
user: CONFIG.SMTP_USERNAME,
|
||||
pass: CONFIG.SMTP_PASSWORD,
|
||||
},
|
||||
})
|
||||
|
||||
transporter.use(
|
||||
'compile',
|
||||
htmlToText({
|
||||
ignoreImage: true,
|
||||
wordwrap: false,
|
||||
}),
|
||||
)
|
||||
await transport.sendMail({
|
||||
from: '"Human Connection" <info@human-connection.org>',
|
||||
...templateArgs,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
sendMail = () => {}
|
||||
if (process.env.NODE_ENV !== 'test') {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('Warning: Email middleware will not try to send mails.')
|
||||
}
|
||||
}
|
||||
|
||||
const transporter = () => {
|
||||
const configs = {
|
||||
host: CONFIG.SMTP_HOST,
|
||||
port: CONFIG.SMTP_PORT,
|
||||
ignoreTLS: CONFIG.SMTP_IGNORE_TLS,
|
||||
secure: false, // true for 465, false for other ports
|
||||
await transporter.sendMail(templateArgs)
|
||||
}
|
||||
const { SMTP_USERNAME: user, SMTP_PASSWORD: pass } = CONFIG
|
||||
if (user && pass) {
|
||||
configs.auth = { user, pass }
|
||||
}
|
||||
return nodemailer.createTransport(configs)
|
||||
}
|
||||
|
||||
const sendSignupMail = async (resolve, root, args, context, resolveInfo) => {
|
||||
@ -50,15 +46,17 @@ const sendSignupMail = async (resolve, root, args, context, resolveInfo) => {
|
||||
return response
|
||||
}
|
||||
|
||||
const sendPasswordResetMail = async (resolve, root, args, context, resolveInfo) => {
|
||||
const { email } = args
|
||||
const { email: emailFound, nonce, name } = await resolve(root, args, context, resolveInfo)
|
||||
const mailTemplate = emailFound ? resetPasswordMail : wrongAccountMail
|
||||
await sendMail(mailTemplate({ email, nonce, name }))
|
||||
return true
|
||||
}
|
||||
|
||||
export default {
|
||||
Mutation: {
|
||||
requestPasswordReset: async (resolve, root, args, context, resolveInfo) => {
|
||||
const { email } = args
|
||||
const { email: emailFound, nonce, name } = await resolve(root, args, context, resolveInfo)
|
||||
const mailTemplate = emailFound ? resetPasswordMail : wrongAccountMail
|
||||
await sendMail(mailTemplate({ email, nonce, name }))
|
||||
return true
|
||||
},
|
||||
requestPasswordReset: sendPasswordResetMail,
|
||||
Signup: sendSignupMail,
|
||||
SignupByInvitation: sendSignupMail,
|
||||
},
|
||||
|
||||
@ -13,6 +13,7 @@ export const resetPasswordMail = options => {
|
||||
actionUrl.searchParams.set('email', email)
|
||||
|
||||
return {
|
||||
from: '"Human Connection" <info@human-connection.org>',
|
||||
to: email,
|
||||
subject,
|
||||
text: `
|
||||
@ -53,6 +54,7 @@ export const wrongAccountMail = options => {
|
||||
} = options
|
||||
const actionUrl = new URL('/password-reset/request', CONFIG.CLIENT_URI)
|
||||
return {
|
||||
from: '"Human Connection" <info@human-connection.org>',
|
||||
to: email,
|
||||
subject,
|
||||
text: `
|
||||
|
||||
@ -1,22 +1,22 @@
|
||||
import CONFIG from '../../../config'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import mustache from 'mustache'
|
||||
import CONFIG from '../../../config'
|
||||
|
||||
const signupHtml = fs.readFileSync(path.join(__dirname, './signup.html'), 'utf-8')
|
||||
|
||||
export const signupTemplate = options => {
|
||||
const {
|
||||
email,
|
||||
nonce,
|
||||
subject = 'Welcome to Human Connection!',
|
||||
supportUrl = 'https://human-connection.org/en/contact/',
|
||||
} = options
|
||||
export const signupTemplate = ({
|
||||
email,
|
||||
nonce,
|
||||
subject = 'Willkommen, Bienvenue, Welcome to Human Connection!',
|
||||
supportUrl = 'https://human-connection.org/en/contact',
|
||||
}) => {
|
||||
const actionUrl = new URL('/registration/create-user-account', CONFIG.CLIENT_URI)
|
||||
actionUrl.searchParams.set('nonce', nonce)
|
||||
actionUrl.searchParams.set('email', email)
|
||||
|
||||
return {
|
||||
from: '"Human Connection" <info@human-connection.org>',
|
||||
to: email,
|
||||
subject,
|
||||
html: mustache.render(signupHtml, { actionUrl, nonce, supportUrl }),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user