From ec0cf36256c7d598c019ac2964b11439a8740e9e Mon Sep 17 00:00:00 2001 From: Alina Beck Date: Wed, 18 Sep 2019 18:00:34 +0100 Subject: [PATCH] refactor email middleware --- .../src/middleware/email/emailMiddleware.js | 66 +++++++++---------- .../email/templates/passwordReset.js | 2 + .../src/middleware/email/templates/signup.js | 16 ++--- 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/backend/src/middleware/email/emailMiddleware.js b/backend/src/middleware/email/emailMiddleware.js index 1a63b2c10..52bf25954 100644 --- a/backend/src/middleware/email/emailMiddleware.js +++ b/backend/src/middleware/email/emailMiddleware.js @@ -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" ', - ...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, }, diff --git a/backend/src/middleware/email/templates/passwordReset.js b/backend/src/middleware/email/templates/passwordReset.js index c977594b5..c8617004b 100644 --- a/backend/src/middleware/email/templates/passwordReset.js +++ b/backend/src/middleware/email/templates/passwordReset.js @@ -13,6 +13,7 @@ export const resetPasswordMail = options => { actionUrl.searchParams.set('email', email) return { + from: '"Human Connection" ', 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" ', to: email, subject, text: ` diff --git a/backend/src/middleware/email/templates/signup.js b/backend/src/middleware/email/templates/signup.js index a6abdf6f2..d67c8f7ae 100644 --- a/backend/src/middleware/email/templates/signup.js +++ b/backend/src/middleware/email/templates/signup.js @@ -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" ', to: email, subject, html: mustache.render(signupHtml, { actionUrl, nonce, supportUrl }),