All files / lib/util mailer.ts

19.61% Statements 10/51
3.33% Branches 1/30
28.57% Functions 2/7
19.61% Lines 10/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121          16x         16x 16x 16x 16x                                                                                             16x 16x 16x                                                                                                 16x   16x              
import Debug from 'debug'
import nodemailer from 'nodemailer'
import swig from 'swig'
import ses from 'nodemailer-ses-transport'
 
const debug = Debug('crowi:lib:mailer')
 
export default crowi => {
  'use strict'
 
  var config = crowi.getConfig()
  var mailConfig: any = {}
  var mailer: any = {}
  var MAIL_TEMPLATE_DIR = crowi.mailDir
 
  function createSMTPClient(option?) {
    var client
 
    debug('createSMTPClient option', option)
    if (!option) {
      option = {
        host: config.crowi['mail:smtpHost'],
        port: config.crowi['mail:smtpPort'],
      }
 
      if (config.crowi['mail:smtpUser'] && config.crowi['mail:smtpPassword']) {
        option.auth = {
          user: config.crowi['mail:smtpUser'],
          pass: config.crowi['mail:smtpPassword'],
        }
      }
      if (option.port === 465) {
        option.secure = true
      }
    }
    option.tls = { rejectUnauthorized: false }
 
    client = nodemailer.createTransport(option)
 
    debug('mailer set up for SMTP', client)
    return client
  }
 
  function createSESClient(option?) {
    var client
 
    if (!option) {
      option = {
        accessKeyId: config.crowi['aws:accessKeyId'],
        secretAccessKey: config.crowi['aws:secretAccessKey'],
      }
    }
 
    client = nodemailer.createTransport(ses(option))
 
    debug('mailer set up for SES', client)
    return client
  }
 
  function initialize() {
    Eif (!config.crowi['mail:from']) {
      mailer = undefined
      return
    }
 
    if (config.crowi['mail:smtpHost'] && config.crowi['mail:smtpPort']) {
      // SMTP 設定がある場合はそれを優先
      mailer = createSMTPClient()
    } else if (config.crowi['aws:accessKeyId'] && config.crowi['aws:secretAccessKey']) {
      // AWS 設定がある場合はSESを設定
      mailer = createSESClient()
    } else {
      mailer = undefined
    }
 
    mailConfig.from = config.crowi['mail:from']
    mailConfig.subject = config.crowi['app:title'] + 'からのメール'
 
    debug('mailer initialized')
  }
 
  function setupMailConfig(overrideConfig) {
    var c = overrideConfig
    var mc: any = {}
    mc = mailConfig
 
    mc.to = c.to
    mc.from = c.from || mailConfig.from
    mc.text = c.text
    mc.subject = c.subject || mailConfig.subject
 
    return mc
  }
 
  function send(config, callback) {
    if (mailer) {
      var templateVars = config.vars || {}
      return swig.renderFile(MAIL_TEMPLATE_DIR + config.template, templateVars, function(err, output) {
        if (err) {
          throw err
        }
 
        config.text = output
        return mailer.sendMail(setupMailConfig(config), callback)
      })
    } else {
      debug('Mailer is not completed to set up. Please set up SMTP or AWS setting.')
      return callback(new Error('Mailer is not completed to set up. Please set up SMTP or AWS setting.'), null)
    }
  }
 
  initialize()
 
  return {
    createSMTPClient: createSMTPClient,
    createSESClient: createSESClient,
    mailer: mailer,
    send: send,
  }
}