From a02d8d0edb0d13076a66bad77d93147b6d0885e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20Hu=C3=9F?= Date: Wed, 5 Oct 2022 11:07:16 +0200 Subject: [PATCH] Get email-template to work the first time --- backend/package.json | 2 + backend/src/emails/README.md | 26 ++++++ .../emails/accountMultiRegistration/html.pug | 22 +++++ .../accountMultiRegistration/subject.pug | 1 + backend/src/emails/sendEmailTranslated.ts | 88 +++++++++++++++++++ backend/src/emails/sendEmailVariants.ts | 20 +++++ backend/src/graphql/resolver/UserResolver.ts | 4 +- backend/src/locales/de.json | 7 ++ backend/src/locales/en.json | 7 ++ backend/yarn.lock | 21 +++++ 10 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 backend/src/emails/README.md create mode 100644 backend/src/emails/accountMultiRegistration/html.pug create mode 100644 backend/src/emails/accountMultiRegistration/subject.pug create mode 100644 backend/src/emails/sendEmailTranslated.ts create mode 100644 backend/src/emails/sendEmailVariants.ts create mode 100644 backend/src/locales/de.json create mode 100644 backend/src/locales/en.json diff --git a/backend/package.json b/backend/package.json index 2541a7f83..a22079287 100644 --- a/backend/package.json +++ b/backend/package.json @@ -18,6 +18,7 @@ "klicktipp": "cross-env TZ=UTC NODE_ENV=development ts-node -r tsconfig-paths/register src/util/klicktipp.ts" }, "dependencies": { + "@types/email-templates": "^10.0.1", "@types/jest": "^27.0.2", "@types/lodash.clonedeep": "^4.5.6", "@types/uuid": "^8.3.4", @@ -38,6 +39,7 @@ "log4js": "^6.4.6", "mysql2": "^2.3.0", "nodemailer": "^6.6.5", + "pug": "^3.0.2", "random-bigint": "^0.0.1", "reflect-metadata": "^0.1.13", "sodium-native": "^3.3.0", diff --git a/backend/src/emails/README.md b/backend/src/emails/README.md new file mode 100644 index 000000000..1f9433ed8 --- /dev/null +++ b/backend/src/emails/README.md @@ -0,0 +1,26 @@ +# Using `forwardemail`–`email-templates` With `pug` Package + +You'll find the GitHub repository of the `email-templates` package and the `pug` package here: + +- [email-templates](https://github.com/forwardemail/email-templates) +- [pug](https://www.npmjs.com/package/pug) + +## Installation + +To install both packages with yarn run: + +```bash +yarn add email-templates pug +``` + +## `pug` Documentation + +The full `pug` documentation you'll find here: + +- [pugjs.org](https://pugjs.org/) + +### Caching Possibility + +In case we are sending many emails in the future there is the possibility to cache the `pug` templates: + +- [cache-pug-templates](https://github.com/ladjs/cache-pug-templates) diff --git a/backend/src/emails/accountMultiRegistration/html.pug b/backend/src/emails/accountMultiRegistration/html.pug new file mode 100644 index 000000000..e1384bf53 --- /dev/null +++ b/backend/src/emails/accountMultiRegistration/html.pug @@ -0,0 +1,22 @@ +doctype html +html(lang="en") + head + title= subject + body + h1 Gradido: Erneuter Registrierungsversuch mit deiner E-Mail + #container.col + p Hallo #{firstName} #{lastName}, + br + p Deine E-Mail-Adresse wurde soeben erneut benutzt, um bei Gradido ein Konto zu registrieren. + br + | Es existiert jedoch zu deiner E-Mail-Adresse schon ein Konto. + p Klicke bitte auf den folgenden Link, falls du dein Passwort vergessen haben solltest: + br + a(href=resendLink) #{resendLink} + br + | oder kopiere den obigen Link in dein Browserfenster. + p Wenn du nicht derjenige bist, der sich versucht hat erneut zu registrieren, wende dich bitte an unseren support: + a(href='https://gradido.net/de/contact/') https://gradido.net/de/contact/ + p Mit freundlichen Grüßen, + br + | dein Gradido-Team diff --git a/backend/src/emails/accountMultiRegistration/subject.pug b/backend/src/emails/accountMultiRegistration/subject.pug new file mode 100644 index 000000000..03faf8e29 --- /dev/null +++ b/backend/src/emails/accountMultiRegistration/subject.pug @@ -0,0 +1 @@ += `Gradido: Erneuter Registrierungsversuch mit deiner E-Mail` \ No newline at end of file diff --git a/backend/src/emails/sendEmailTranslated.ts b/backend/src/emails/sendEmailTranslated.ts new file mode 100644 index 000000000..084db1232 --- /dev/null +++ b/backend/src/emails/sendEmailTranslated.ts @@ -0,0 +1,88 @@ +import { backendLogger as logger } from '@/server/logger' +import { createTransport } from 'nodemailer' +import Email from 'email-templates' + +import CONFIG from '@/config' + +export const sendEmailTranslated = async (params: { + receiver: { + to: string + cc?: string + } + template: string + locals: Record +}): Promise => { + // Wolle: logger.info( + // `send Email: to=${params.receiver.to}` + + // (params.receiver.cc ? `, cc=${params.receiver.cc}` : '') + + // `, subject=${params.locals.subject}, text=${params.text}`, + // ) + logger.info( + `send Email: to=${params.receiver.to}` + + (params.receiver.cc ? `, cc=${params.receiver.cc}` : '') + + `, subject=${params.locals.subject}`, + ) + // Wolle: console.log('sendEmailTranslated !!!') + // Wolle: console.log('params: ', params) + + if (!CONFIG.EMAIL) { + logger.info(`Emails are disabled via config...`) + return false + } + if (CONFIG.EMAIL_TEST_MODUS) { + logger.info( + `Testmodus=ON: change receiver from ${params.receiver.to} to ${CONFIG.EMAIL_TEST_RECEIVER}`, + ) + params.receiver.to = CONFIG.EMAIL_TEST_RECEIVER + } + const transport = 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 email = new Email({ + message: { + from: `Gradido (nicht antworten) <${CONFIG.EMAIL_SENDER}>`, + }, + // uncomment below to send emails in development/test env: + // send: true, + // Wolle: transport: { + // jsonTransport: true, + // }, + transport, + // uncomment below to open send emails in the browser + // Wolle: + // preview: { + // open: { + // app: 'firefox', + // wait: false, + // }, + // }, + }) + + email + .send({ + template: '/app/src/emails/' + params.template, + message: { + ...params.receiver, + }, + // Wolle: locals: params.locals, + locals: { ...params.locals, locale: 'de' }, + }) + .then((result: unknown) => { + logger.info('Send email successfully.') + logger.info('Result: ', result) + }) + .catch((error: unknown) => { + logger.error('Error sending notification email: ', error) + throw new Error('Error sending notification email!') + }) + + return true +} diff --git a/backend/src/emails/sendEmailVariants.ts b/backend/src/emails/sendEmailVariants.ts new file mode 100644 index 000000000..742e33c6a --- /dev/null +++ b/backend/src/emails/sendEmailVariants.ts @@ -0,0 +1,20 @@ +import { sendEmailTranslated } from './sendEmailTranslated' +import CONFIG from '@/config' + +export const sendAccountMultiRegistrationEmail = (data: { + firstName: string + lastName: string + email: string +}): Promise => { + // Wolle: console.log('sendAccountMultiRegistrationEmail !!!') + return sendEmailTranslated({ + receiver: { to: `${data.firstName} ${data.lastName} <${data.email}>` }, + template: 'accountMultiRegistration', + locals: { + subject: 'Gradido: Erneuter Registrierungsversuch mit deiner E-Mail', + firstName: data.firstName, + lastName: data.lastName, + resendLink: CONFIG.EMAIL_LINK_FORGOTPASSWORD, + }, + }) +} diff --git a/backend/src/graphql/resolver/UserResolver.ts b/backend/src/graphql/resolver/UserResolver.ts index 5ad578767..ccf429567 100644 --- a/backend/src/graphql/resolver/UserResolver.ts +++ b/backend/src/graphql/resolver/UserResolver.ts @@ -18,7 +18,8 @@ import { klicktippNewsletterStateMiddleware } from '@/middleware/klicktippMiddle import { OptInType } from '@enum/OptInType' import { sendResetPasswordEmail as sendResetPasswordEmailMailer } from '@/mailer/sendResetPasswordEmail' import { sendAccountActivationEmail } from '@/mailer/sendAccountActivationEmail' -import { sendAccountMultiRegistrationEmail } from '@/mailer/sendAccountMultiRegistrationEmail' +// Wolle: import { sendAccountMultiRegistrationEmail } from '@/mailer/sendAccountMultiRegistrationEmail' +import { sendAccountMultiRegistrationEmail } from '@/emails/sendEmailVariants' import { klicktippSignIn } from '@/apis/KlicktippController' import { RIGHTS } from '@/auth/RIGHTS' import { hasElopageBuys } from '@/util/hasElopageBuys' @@ -429,6 +430,7 @@ export class UserResolver { user.publisherId = publisherId logger.debug('partly faked user=' + user) + // Wolle: console.log('createUser !!! already exists …') // eslint-disable-next-line @typescript-eslint/no-unused-vars const emailSent = await sendAccountMultiRegistrationEmail({ firstName, diff --git a/backend/src/locales/de.json b/backend/src/locales/de.json new file mode 100644 index 000000000..4bb1f44e8 --- /dev/null +++ b/backend/src/locales/de.json @@ -0,0 +1,7 @@ +{ + "emails": { + "accountMultiRegistration": { + "subject": "Gradido: Erneuter Registrierungsversuch mit deiner E-Mail" + } + } +} \ No newline at end of file diff --git a/backend/src/locales/en.json b/backend/src/locales/en.json new file mode 100644 index 000000000..81200ac88 --- /dev/null +++ b/backend/src/locales/en.json @@ -0,0 +1,7 @@ +{ + "emails": { + "accountMultiRegistration": { + "subject": "Gradido: Try To Register Again With Your Email" + } + } +} \ No newline at end of file diff --git a/backend/yarn.lock b/backend/yarn.lock index 3c4851611..22db4547d 100644 --- a/backend/yarn.lock +++ b/backend/yarn.lock @@ -902,6 +902,15 @@ resolved "https://registry.yarnpkg.com/@types/cors/-/cors-2.8.10.tgz#61cc8469849e5bcdd0c7044122265c39cec10cf4" integrity sha512-C7srjHiVG3Ey1nR6d511dtDkCEjxuN9W1HWAEjGq8kpcwmNM6JJkpC0xvabM7BXTG2wDq8Eu33iH9aQKa7IvLQ== +"@types/email-templates@^10.0.1": + version "10.0.1" + resolved "https://registry.yarnpkg.com/@types/email-templates/-/email-templates-10.0.1.tgz#88f218564a6341092f447fbe110047f6bf3e955a" + integrity sha512-IHdgtoOUfMB4t5y5wgm8G0i2/U90GeJPxIEAViMaLlJPCJzaYSlVHXI8bx3qbgbD6gxyOsSRyrFvBSTgNEQc+g== + dependencies: + "@types/html-to-text" "*" + "@types/nodemailer" "*" + juice "^8.0.0" + "@types/express-serve-static-core@^4.17.18", "@types/express-serve-static-core@^4.17.21": version "4.17.24" resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.24.tgz#ea41f93bf7e0d59cd5a76665068ed6aab6815c07" @@ -948,6 +957,11 @@ dependencies: "@types/node" "*" +"@types/html-to-text@*": + version "8.1.1" + resolved "https://registry.yarnpkg.com/@types/html-to-text/-/html-to-text-8.1.1.tgz#0c5573207c14f618f24da5a2910c510285573094" + integrity sha512-QFcqfc7TiVbvIX8Fc2kWUxakruI1Ay6uitaGCYHzI5M0WHQROV5D2XeSaVrK0FmvssivXum4yERVnJsiuH61Ww== + "@types/http-assert@*": version "1.5.3" resolved "https://registry.yarnpkg.com/@types/http-assert/-/http-assert-1.5.3.tgz#ef8e3d1a8d46c387f04ab0f2e8ab8cb0c5078661" @@ -1070,6 +1084,13 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-14.17.21.tgz#6359d8cf73481e312a43886fa50afc70ce5592c6" integrity sha512-zv8ukKci1mrILYiQOwGSV4FpkZhyxQtuFWGya2GujWg+zVAeRQ4qbaMmWp9vb9889CFA8JECH7lkwCL6Ygg8kA== +"@types/nodemailer@*": + version "6.4.6" + resolved "https://registry.yarnpkg.com/@types/nodemailer/-/nodemailer-6.4.6.tgz#ce21b4b474a08f672f182e15982b7945dde1f288" + integrity sha512-pD6fL5GQtUKvD2WnPmg5bC2e8kWCAPDwMPmHe/ohQbW+Dy0EcHgZ2oCSuPlWNqk74LS5BVMig1SymQbFMPPK3w== + dependencies: + "@types/node" "*" + "@types/nodemailer@^6.4.4": version "6.4.4" resolved "https://registry.yarnpkg.com/@types/nodemailer/-/nodemailer-6.4.4.tgz#c265f7e7a51df587597b3a49a023acaf0c741f4b"