unite email templates in templateBuilder

This commit is contained in:
Alina Beck 2019-09-19 01:15:52 +01:00
parent a7d525deab
commit e843edeb13
5 changed files with 57 additions and 152 deletions

View File

@ -1,13 +1,16 @@
import CONFIG from '../../config' import CONFIG from '../../config'
import nodemailer from 'nodemailer' import nodemailer from 'nodemailer'
import { htmlToText } from 'nodemailer-html-to-text' import { htmlToText } from 'nodemailer-html-to-text'
import { resetPasswordTemplate } from './templates/passwordReset' import {
import { signupTemplate } from './templates/signup' signupTemplate,
resetPasswordTemplate,
wrongAccountTemplate,
} from './templates/templateBuilder'
const hasEmailConfig = CONFIG.SMTP_HOST && CONFIG.SMTP_PORT const hasEmailConfig = CONFIG.SMTP_HOST && CONFIG.SMTP_PORT
const hasAuthData = CONFIG.SMTP_USERNAME && CONFIG.SMTP_PASSWORD const hasAuthData = CONFIG.SMTP_USERNAME && CONFIG.SMTP_PASSWORD
let sendMail = () => { } let sendMail = () => {}
if (!hasEmailConfig) { if (!hasEmailConfig) {
if (process.env.NODE_ENV !== 'test') { if (process.env.NODE_ENV !== 'test') {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
@ -48,8 +51,9 @@ const sendSignupMail = async (resolve, root, args, context, resolveInfo) => {
const sendPasswordResetMail = async (resolve, root, args, context, resolveInfo) => { const sendPasswordResetMail = async (resolve, root, args, context, resolveInfo) => {
const { email } = args const { email } = args
const { email: emailFound, nonce, name } = await resolve(root, args, context, resolveInfo) const { email: userFound, nonce, name } = await resolve(root, args, context, resolveInfo)
await sendMail(resetPasswordTemplate({ emailFound, email, nonce, name })) const template = userFound ? resetPasswordTemplate : wrongAccountTemplate
await sendMail(template({ email, nonce, name }))
return true return true
} }

View File

@ -1,38 +0,0 @@
import fs from 'fs'
import path from 'path'
import mustache from 'mustache'
import CONFIG from '../../../config'
const passwordResetHtml = fs.readFileSync(path.join(__dirname, './resetPassword.html'), 'utf-8')
const wrongAccountHtml = fs.readFileSync(path.join(__dirname, './wrongAccount.html'), 'utf-8')
const supportUrl = 'https://human-connection.org/en/contact/'
const from = '"Human Connection" <info@human-connection.org>'
export const resetPasswordTemplate = ({
name,
email,
emailFound,
nonce,
}) => {
let subject, htmlTemplate, actionUrl
if (emailFound) {
subject = 'Neues Passwort | Reset Password'
htmlTemplate = passwordResetHtml
actionUrl = new URL('/password-reset/change-password', CONFIG.CLIENT_URI)
actionUrl.searchParams.set('nonce', nonce)
actionUrl.searchParams.set('email', email)
} else {
subject = 'Falsche Mailadresse? | Wrong Email?'
htmlTemplate = wrongAccountHtml
actionUrl = new URL('/password-reset/request', CONFIG.CLIENT_URI)
}
return {
from,
to: email,
subject,
html: mustache.render(htmlTemplate, { actionUrl, name, nonce, supportUrl }),
}
}

View File

@ -1,24 +0,0 @@
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 = ({
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 }),
}
}

View File

@ -0,0 +1,48 @@
import fs from 'fs'
import path from 'path'
import mustache from 'mustache'
import CONFIG from '../../../config'
const from = '"Human Connection" <info@human-connection.org>'
const supportUrl = 'https://human-connection.org/en/contact'
const signupHtml = fs.readFileSync(path.join(__dirname, './signup.html'), 'utf-8')
const passwordResetHtml = fs.readFileSync(path.join(__dirname, './resetPassword.html'), 'utf-8')
const wrongAccountHtml = fs.readFileSync(path.join(__dirname, './wrongAccount.html'), 'utf-8')
export const signupTemplate = ({ email, nonce }) => {
const actionUrl = new URL('/registration/create-user-account', CONFIG.CLIENT_URI)
actionUrl.searchParams.set('nonce', nonce)
actionUrl.searchParams.set('email', email)
return {
from,
to: email,
subject: 'Willkommen, Bienvenue, Welcome to Human Connection!',
html: mustache.render(signupHtml, { actionUrl, supportUrl }),
}
}
export const resetPasswordTemplate = ({ email, nonce, name }) => {
const actionUrl = new URL('/password-reset/change-password', CONFIG.CLIENT_URI)
actionUrl.searchParams.set('nonce', nonce)
actionUrl.searchParams.set('email', email)
return {
from,
to: email,
subject: 'Neues Passwort | Reset Password',
html: mustache.render(passwordResetHtml, { actionUrl, name, nonce, supportUrl }),
}
}
export const wrongAccountTemplate = ({ email }) => {
const actionUrl = new URL('/password-reset/request', CONFIG.CLIENT_URI)
return {
from,
to: email,
subject: 'Falsche Mailadresse? | Wrong Email?',
html: mustache.render(wrongAccountHtml, { actionUrl, supportUrl }),
}
}

View File

@ -1,85 +0,0 @@
import CONFIG from '../../../config'
export const from = '"Human Connection" <info@human-connection.org>'
export const resetPasswordMail = options => {
const {
name,
email,
code,
subject = 'Use this link to reset your password. The link is only valid for 24 hours.',
supportUrl = 'https://human-connection.org/en/contact/',
} = options
const actionUrl = new URL('/password-reset/change-password', CONFIG.CLIENT_URI)
actionUrl.searchParams.set('code', code)
actionUrl.searchParams.set('email', email)
return {
to: email,
subject,
text: `
Hi ${name}!
You recently requested to reset your password for your Human Connection account.
Use the link below to reset it. This password reset is only valid for the next
24 hours.
${actionUrl}
If you did not request a password reset, please ignore this email or contact
support if you have questions:
${supportUrl}
Thanks,
The Human Connection Team
If you're having trouble with the link above, you can manually copy and
paste the following code into your browser window:
${code}
Human Connection gemeinnützige GmbH
Bahnhofstr. 11
73235 Weilheim / Teck
Deutschland
`,
}
}
export const wrongAccountMail = options => {
const {
email,
subject = `We received a request to reset your password with this email address (${email})`,
supportUrl = 'https://human-connection.org/en/contact/',
} = options
const actionUrl = new URL('/password-reset/request', CONFIG.CLIENT_URI)
return {
to: email,
subject,
text: `
We received a request to reset the password to access Human Connection with your
email address, but we were unable to find an account associated with this
address.
If you use Human Connection and were expecting this email, consider trying to
request a password reset using the email address associated with your account.
Try a different email:
${actionUrl}
If you do not use Human Connection or did not request a password reset, please
ignore this email. Feel free to contact support if you have further questions:
${supportUrl}
Thanks,
The Human Connection Team
Human Connection gemeinnützige GmbH
Bahnhofstr. 11
73235 Weilheim / Teck
Deutschland
`,
}
}