Get email-template to work the first time

This commit is contained in:
Wolfgang Huß 2022-10-05 11:07:16 +02:00
parent 47e4be4697
commit a02d8d0edb
10 changed files with 197 additions and 1 deletions

View File

@ -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",

View File

@ -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)

View File

@ -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

View File

@ -0,0 +1 @@
= `Gradido: Erneuter Registrierungsversuch mit deiner E-Mail`

View File

@ -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<string, unknown>
}): Promise<boolean> => {
// 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
}

View File

@ -0,0 +1,20 @@
import { sendEmailTranslated } from './sendEmailTranslated'
import CONFIG from '@/config'
export const sendAccountMultiRegistrationEmail = (data: {
firstName: string
lastName: string
email: string
}): Promise<boolean> => {
// 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,
},
})
}

View File

@ -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,

View File

@ -0,0 +1,7 @@
{
"emails": {
"accountMultiRegistration": {
"subject": "Gradido: Erneuter Registrierungsversuch mit deiner E-Mail"
}
}
}

View File

@ -0,0 +1,7 @@
{
"emails": {
"accountMultiRegistration": {
"subject": "Gradido: Try To Register Again With Your Email"
}
}
}

View File

@ -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"