Skip to content

Commit

Permalink
Feat/switch email sender provider (#1005)
Browse files Browse the repository at this point in the history
* feat(loopback): prototype send email via gworkspace

* feat(nodejs): send via GWorkspace here too

* switch sender for sweden emaisl
  • Loading branch information
ericbolikowski authored Jan 23, 2025
1 parent 1d58eeb commit 4478f73
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 82 deletions.
49 changes: 8 additions & 41 deletions apps/nestjs-api/src/email/lib/email/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,25 @@

import { buildFrontendUrl } from '../build-frontend-url'

const aws = require('aws-sdk')
const Rx = require('rxjs')
const mjml2html = require('mjml')
const nodemailer = require('nodemailer')
const fs = require('fs')
const path = require('path')

const config = {
accessKeyId: process.env.NX_EMAILER_AWS_ACCESS_KEY,
secretAccessKey: process.env.NX_EMAILER_AWS_SECRET_KEY,
region: process.env.NX_EMAILER_AWS_REGION,
}

const ses = new aws.SES(config)

const transporter = nodemailer.createTransport({
SES: ses,
host: 'smtp.googlemail.com',
port: 465,
secure: true,
auth: {
user: '[email protected]',
pass: process.env.NX_GWORKSPACE_EMAIL_PASSWORD,
},
})

const isProductionOrDemonstration = () =>
['production', 'demonstration', 'staging'].includes(process.env.NODE_ENV)

export const sendEmail = Rx.bindNodeCallback(ses.sendEmail.bind(ses))
export const sendMjmlEmail = Rx.bindNodeCallback(
transporter.sendMail.bind(transporter)
)
Expand All @@ -36,40 +32,11 @@ const getSenderDetails = (rediLocation) => {
? 'ReDI Malmö Team'
: 'ReDI Talent Success Team'
const senderEmail = isMalmoLocation
? 'career.sweden@redi-school.org'
? '[email protected]' // TODO: set back to career-sweden when we send email via Azure
: '[email protected]'
return { senderName, senderEmail }
}

export const sendEmailFactory = (to, subject, body, rediLocation) => {
let toSanitized = isProductionOrDemonstration() ? to : ''
if (process.env.NX_DEV_MODE_EMAIL_RECIPIENT) {
toSanitized = process.env.NX_DEV_MODE_EMAIL_RECIPIENT
}

const { senderName, senderEmail } = getSenderDetails(rediLocation)

return sendEmail({
Source: `${senderName} <${senderEmail}>`,
Destination: {
ToAddresses: [toSanitized],
BccAddresses: [`${senderName} <${senderEmail}>`],
},
Message: {
Body: {
Text: {
Charset: 'UTF-8',
Data: body,
},
},
Subject: {
Charset: 'UTF-8',
Data: buildSubjectLine(subject, process.env.NODE_ENV),
},
},
})
}

export const sendMjmlEmailFactory = ({ to, subject, html, rediLocation }) => {
let toSanitized = isProductionOrDemonstration() ? to : ''
if (process.env.NX_DEV_MODE_EMAIL_RECIPIENT) {
Expand Down
48 changes: 7 additions & 41 deletions loopback-standalone/lib/email/email.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
'use strict'

const aws = require('aws-sdk')
const Rx = require('rxjs')
const mjml2html = require('mjml')
const nodemailer = require('nodemailer')
const fs = require('fs')
const path = require('path')

const config = {
accessKeyId: process.env.NX_EMAILER_AWS_ACCESS_KEY,
secretAccessKey: process.env.NX_EMAILER_AWS_SECRET_KEY,
region: process.env.NX_EMAILER_AWS_REGION,
}
const { buildFrontendUrl } = require('../build-frontend-url')
const { buildBackendUrl } = require('../build-backend-url')
const { ConsoleLogger } = require('@nestjs/common')

const ses = new aws.SES(config)

const transporter = nodemailer.createTransport({
SES: ses,
host: 'smtp.googlemail.com',
port: 465,
secure: true,
auth: {
user: '[email protected]',
pass: process.env.NX_GWORKSPACE_EMAIL_PASSWORD,
},
})

const isProductionOrDemonstration = () =>
['production', 'demonstration', 'staging'].includes(process.env.NODE_ENV)

const sendEmail = Rx.bindNodeCallback(ses.sendEmail.bind(ses))
const sendMjmlEmail = Rx.bindNodeCallback(
transporter.sendMail.bind(transporter)
)
Expand All @@ -42,35 +38,6 @@ const getSenderDetails = (rediLocation) => {
return { senderName, senderEmail }
}

const sendEmailFactory = (to, subject, body, rediLocation) => {
let toSanitized = isProductionOrDemonstration() ? to : ''
if (process.env.NX_DEV_MODE_EMAIL_RECIPIENT) {
toSanitized = process.env.NX_DEV_MODE_EMAIL_RECIPIENT
}

const { senderName, senderEmail } = getSenderDetails(rediLocation)

return sendEmail({
Source: `${senderName} <${senderEmail}>`,
Destination: {
ToAddresses: [toSanitized],
BccAddresses: [`${senderName} <${senderEmail}>`],
},
Message: {
Body: {
Text: {
Charset: 'UTF-8',
Data: body,
},
},
Subject: {
Charset: 'UTF-8',
Data: buildSubjectLine(subject, process.env.NODE_ENV),
},
},
})
}

const sendMjmlEmailFactory = ({ to, subject, html, rediLocation }) => {
let toSanitized = isProductionOrDemonstration() ? to : ''
if (process.env.NX_DEV_MODE_EMAIL_RECIPIENT) {
Expand Down Expand Up @@ -172,7 +139,6 @@ const sendConVerificationEmail = ({

module.exports = {
sendResetPasswordEmail,
sendEmailFactory,
sendMjmlEmailFactory,
sendConVerificationEmail,
}

0 comments on commit 4478f73

Please sign in to comment.