+ const fromDisplayName = options.from
+ ? options.from
+ : CONFIG.INSTANCE.NAME
+
+ const email = new Email({
+ send: true,
+ message: {
+ from: `"${fromDisplayName}" <${CONFIG.SMTP.FROM_ADDRESS}>`
+ },
+ transport: this.transporter,
+ views: {
+ root: join(root(), 'dist', 'server', 'lib', 'emails')
+ },
+ subjectPrefix: CONFIG.EMAIL.SUBJECT.PREFIX
+ })
+
+ const toEmails = isArray(options.to)
+ ? options.to
+ : [ options.to ]
+
+ for (const to of toEmails) {
+ const baseOptions: SendEmailDefaultOptions = {
+ template: 'common',
+ message: {
+ to,
+ from: options.from,
+ subject: options.subject,
+ replyTo: options.replyTo
+ },
+ locals: { // default variables available in all templates
+ WEBSERVER,
+ EMAIL: CONFIG.EMAIL,
+ instanceName: CONFIG.INSTANCE.NAME,
+ text: options.text,
+ subject: options.subject
+ }
+ }
+
+ // overriden/new variables given for a specific template in the payload
+ const sendOptions = merge(baseOptions, options)
+
+ await email.send(sendOptions)
+ .then(res => logger.debug('Sent email.', { res }))
+ .catch(err => logger.error('Error in email sender.', { err }))
+ }
+ }
+
+ private warnOnConnectionFailure (err?: Error) {
+ logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err })
+ }
+
+ private initSMTPTransport () {
+ logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT)
+
+ let tls
+ if (CONFIG.SMTP.CA_FILE) {
+ tls = {
+ ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ]
+ }
+ }
+
+ let auth
+ if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) {
+ auth = {
+ user: CONFIG.SMTP.USERNAME,
+ pass: CONFIG.SMTP.PASSWORD
+ }
+ }
+
+ this.transporter = createTransport({
+ host: CONFIG.SMTP.HOSTNAME,
+ port: CONFIG.SMTP.PORT,
+ secure: CONFIG.SMTP.TLS,
+ debug: CONFIG.LOG.LEVEL === 'debug',
+ logger: bunyanLogger as any,
+ ignoreTLS: CONFIG.SMTP.DISABLE_STARTTLS,
+ tls,
+ auth