Skip to content

Commit

Permalink
Add configuration for email service
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkvon committed Oct 11, 2023
1 parent 094d40b commit ad9c4af
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
17 changes: 17 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,20 @@ BASE_URL=http://localhost:3005
MAILER_IDENTITY_EMAIL=bot@example
MAILER_IDENTITY_PASSWORD=password
MAILER_IDENTITY_PROVIDER=http://localhost:3456

# SMTP Transport for sending emails
# string or undefined
SMTP_TRANSPORT_HOST=
# number
SMTP_TRANSPORT_PORT=1025
# boolean - give truthy value if true, otherwise leave empty
SMTP_TRANSPORT_SECURE=
# string or undefined
SMTP_TRANSPORT_AUTH_USER=
# string or undefined
SMTP_TRANSPORT_AUTH_PASS=
# boolean
SMTP_TRANSPORT_REQUIRE_TLS=

# email address which will be the sender of the notifications and email verification messages
EMAIL_SENDER=[email protected]
24 changes: 24 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
import 'dotenv/config'
import SMTPTransport from 'nodemailer/lib/smtp-transport'

// the defaults work for tests. you should define your own
// either via .env file, or via environment variables directly (depends on your setup)

// server base url, e.g. to construct correct email verification links
export const baseUrl = process.env.BASE_URL ?? 'http://localhost:3005'

// identity under which the mailer is operating
export const mailerCredentials = {
email: process.env.MAILER_IDENTITY_EMAIL ?? 'bot@example',
password: process.env.MAILER_IDENTITY_PASSWORD ?? 'password',
solidServer: process.env.MAILER_IDENTITY_PROVIDER ?? 'http://localhost:3456',
}

const stringToBoolean = (value: string | undefined): boolean => {
if (value === 'false') return false
if (value === '0') return false
return !!value
}
// SMTP transport for nodemailer (setup for sending emails)
export const smtpTransportOptions: SMTPTransport.Options = {
host: process.env.SMTP_TRANSPORT_HOST || undefined,
port: process.env.SMTP_TRANSPORT_PORT
? +process.env.SMTP_TRANSPORT_PORT
: 1025, // default works for maildev
secure: stringToBoolean(process.env.SMTP_TRANSPORT_SECURE),
auth: {
user: process.env.SMTP_TRANSPORT_AUTH_USER || undefined,
pass: process.env.SMTP_TRANSPORT_AUTH_PASS || undefined,
},
requireTLS: stringToBoolean(process.env.STP_TRANSPORT_REQUIRE_TLS),
}

// email address which will be the sender of the notifications and email verification messages
export const emailSender = process.env.EMAIL_SENDER

export const port: number = +(process.env.PORT ?? 3005)

// email verification expiration in milliseconds (1 hour)
Expand Down
1 change: 1 addition & 0 deletions src/controllers/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const initializeIntegration: Middleware = async ctx => {
}/verify-email?id=${encodeURIComponent(webId)}&token=${token}`

await sendMail({
from: config.emailSender,
to: email,
subject: 'TODO',
html: `Please verify your email <a href="${emailVerificationLink}">click here</a>`,
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/webhookReceiver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Middleware } from 'koa'
import { emailSender } from '../config'
import { Integration } from '../config/sequelize'
import { sendMail } from '../services/mailerService'

Expand All @@ -20,6 +21,7 @@ export const webhookReceiver: Middleware = async ctx => {
)

await sendMail({
from: emailSender,
to: integration.email,
subject: 'TODO',
html: 'TODO',
Expand Down
3 changes: 2 additions & 1 deletion src/services/mailerService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as nodemailer from 'nodemailer'
import Mail from 'nodemailer/lib/mailer'
import { smtpTransportOptions } from '../config'

export const sendMail = async (options: Mail.Options) => {
const smtpTransport = nodemailer.createTransport({ port: 1025 })
const smtpTransport = nodemailer.createTransport(smtpTransportOptions)
await smtpTransport.sendMail(options)
}

0 comments on commit ad9c4af

Please sign in to comment.