Skip to content

Commit

Permalink
Email service: allow specifying custom transport options (#3467)
Browse files Browse the repository at this point in the history
* Mailer: allow specifying custom transport options

* Mailer: allow specifying custom email transport options

* updated env template file

* code cleanup

---------

Co-authored-by: Stefano Ricci <SteRiccio@users.noreply.github.com>
  • Loading branch information
SteRiccio and SteRiccio committed Jun 3, 2024
1 parent d786abf commit aae0816
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ EMAIL_SERVICE=sendgrid
SENDGRID_API_KEY=
# EMAIL_AUTH_USER=
# EMAIL_AUTH_PASSWORD=
# Optional: custom email transport options could be specified.
# EMAIL_TRANSPORT_OPTIONS=
# e.g. (for MS office365 service) EMAIL_TRANSPORT_OPTIONS={"host":"smtp.office365.com","port":"587","auth":{"user":"testuser@mydomain.org","pass":"yoursecretpassword"},"secure":true,"tls":{"ciphers":"SSLv3"}}

# Analysis
ANALYSIS_OUTPUT_DIR=/tmp/arena_analysis
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ EMAIL_SERVICE=sendgrid
SENDGRID_API_KEY= # get it from https://sendgrid.com/
# EMAIL_AUTH_USER=
# EMAIL_AUTH_PASSWORD=
# Optional: custom email transport options could be specified.
# EMAIL_TRANSPORT_OPTIONS=
# e.g. (for MS office365 service) EMAIL_TRANSPORT_OPTIONS={"host":"smtp.office365.com","port":"587","auth":{"user":"testuser@mydomain.org","pass":"yoursecretpassword"},"secure":true,"tls":{"ciphers":"SSLv3"}}

# Analysis
ANALYSIS_OUTPUT_DIR=/home/your_user/openforis/arena/analysis
Expand Down
9 changes: 9 additions & 0 deletions core/processUtils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
const isTrue = (val) => String(val).toLocaleLowerCase() === 'true' || String(val) === '1'
const getJson = (val) => {
if (!val) return undefined
try {
return JSON.parse(val)
} catch (error) {
return undefined
}
}

const environments = {
development: 'development',
Expand Down Expand Up @@ -34,6 +42,7 @@ const ENV = {
emailService: process.env.EMAIL_SERVICE || 'sendgrid',
emailAuthUser: process.env.EMAIL_AUTH_USER,
emailAuthPassword: process.env.EMAIL_AUTH_PASSWORD,
emailTransportOptions: getJson(process.env.EMAIL_TRANSPORT_OPTIONS),
sendGridApiKey: process.env.SENDGRID_API_KEY,
// ANALYSIS
analysisOutputDir: process.env.ANALYSIS_OUTPUT_DIR,
Expand Down
13 changes: 12 additions & 1 deletion server/utils/mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import * as nodemailer from 'nodemailer'
import * as ProcessUtils from '@core/processUtils'
import * as i18nFactory from '@core/i18n/i18nFactory'

import * as Log from '@server/log/log'

const logger = Log.getLogger('Mailer')

const emailServices = {
sendgrid: 'sendgrid',
office365: 'office365',
Expand All @@ -19,14 +23,21 @@ const authUser = ProcessUtils.ENV.emailAuthUser
const authPass = ProcessUtils.ENV.emailAuthPassword
const from = ProcessUtils.ENV.adminEmail || authUser

const office365TransportOptions = {
const defaultOffice365TransportOptions = {
host: 'smtp.office365.com',
port: '587',
auth: { user: authUser, pass: authPass },
secure: true,
tls: { ciphers: 'SSLv3' },
}

let office365TransportOptions = null
{
const optionsType = ProcessUtils.ENV.emailTransportOptions ? 'custom' : 'default'
logger.debug(`using ${optionsType} email transport options`)
office365TransportOptions = ProcessUtils.ENV.emailTransportOptions ?? defaultOffice365TransportOptions
}

const sendEmailSendgrid = async ({ to, subject, html }) => {
if (Array.isArray(to)) {
await sgMail.sendMultiple({ to, from, subject, html })
Expand Down

0 comments on commit aae0816

Please sign in to comment.