2014-03-04 13:10:06 -05:00
|
|
|
logger = require('logger-sharelatex')
|
|
|
|
metrics = require('../../infrastructure/Metrics')
|
|
|
|
Settings = require('settings-sharelatex')
|
2014-03-06 10:37:25 -05:00
|
|
|
nodemailer = require("nodemailer")
|
2016-02-12 12:13:45 -05:00
|
|
|
sesTransport = require('nodemailer-ses-transport')
|
2016-03-29 09:15:33 -04:00
|
|
|
sgTransport = require('nodemailer-sendgrid-transport')
|
|
|
|
|
2016-02-12 12:13:45 -05:00
|
|
|
_ = require("underscore")
|
2014-03-04 13:10:06 -05:00
|
|
|
|
2014-03-05 17:37:55 -05:00
|
|
|
if Settings.email? and Settings.email.fromAddress?
|
|
|
|
defaultFromAddress = Settings.email.fromAddress
|
2014-03-06 10:37:25 -05:00
|
|
|
else
|
2014-03-05 17:57:28 -05:00
|
|
|
defaultFromAddress = ""
|
2014-03-05 06:57:57 -05:00
|
|
|
|
2014-03-06 10:37:25 -05:00
|
|
|
# provide dummy mailer unless we have a better one configured.
|
|
|
|
client =
|
|
|
|
sendMail: (options, callback = (err,status) ->) ->
|
|
|
|
logger.log options:options, "Would send email if enabled."
|
|
|
|
callback()
|
2014-03-04 13:10:06 -05:00
|
|
|
|
2016-02-12 12:13:45 -05:00
|
|
|
if Settings?.email?.parameters?.AWSAccessKeyID?
|
|
|
|
logger.log "using aws ses for email"
|
|
|
|
nm_client = nodemailer.createTransport(sesTransport(Settings.email.parameters))
|
2016-03-29 09:15:33 -04:00
|
|
|
else if Settings?.email?.parameters?.sendgridApiKey?
|
|
|
|
logger.log "using sendgrid for email"
|
|
|
|
nm_client = nodemailer.createTransport(sgTransport({auth:{api_key:Settings?.email?.parameters?.sendgridApiKey}}))
|
2016-02-12 12:13:45 -05:00
|
|
|
else if Settings?.email?.parameters?
|
|
|
|
smtp = _.pick(Settings?.email?.parameters, "host", "port", "secure", "auth")
|
|
|
|
|
|
|
|
|
|
|
|
logger.log "using smtp for email"
|
|
|
|
nm_client = nodemailer.createTransport(smtp)
|
|
|
|
else
|
|
|
|
nm_client = client
|
|
|
|
logger.warn "Email transport and/or parameters not defined. No emails will be sent."
|
|
|
|
|
|
|
|
if nm_client?
|
|
|
|
client = nm_client
|
|
|
|
else
|
|
|
|
logger.warn "Failed to create email transport. Please check your settings. No email will be sent."
|
|
|
|
|
2014-03-04 13:10:06 -05:00
|
|
|
|
2014-03-06 10:37:25 -05:00
|
|
|
module.exports =
|
2014-03-04 13:10:06 -05:00
|
|
|
sendEmail : (options, callback = (error) ->)->
|
2014-03-12 19:37:54 -04:00
|
|
|
logger.log receiver:options.to, subject:options.subject, "sending email"
|
2014-03-04 13:10:06 -05:00
|
|
|
metrics.inc "email"
|
2014-03-06 10:37:25 -05:00
|
|
|
options =
|
2014-03-04 13:10:06 -05:00
|
|
|
to: options.to
|
2014-03-05 17:37:55 -05:00
|
|
|
from: defaultFromAddress
|
2014-03-04 13:10:06 -05:00
|
|
|
subject: options.subject
|
2014-03-12 07:43:36 -04:00
|
|
|
html: options.html
|
2014-03-05 07:06:28 -05:00
|
|
|
replyTo: options.replyTo || Settings.email.replyToAddress
|
2014-06-17 13:18:45 -04:00
|
|
|
socketTimeout: 30 * 1000
|
2014-03-06 10:37:25 -05:00
|
|
|
client.sendMail options, (err, res)->
|
2014-03-04 13:10:06 -05:00
|
|
|
if err?
|
|
|
|
logger.err err:err, "error sending message"
|
|
|
|
else
|
|
|
|
logger.log "Message sent to #{options.to}"
|
|
|
|
callback(err)
|