2014-03-04 13:10:06 -05:00
|
|
|
logger = require('logger-sharelatex')
|
|
|
|
metrics = require('../../infrastructure/Metrics')
|
|
|
|
Settings = require('settings-sharelatex')
|
|
|
|
metrics = require("../../infrastructure/Metrics")
|
2014-03-06 10:37:25 -05:00
|
|
|
nodemailer = require("nodemailer")
|
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
|
|
|
|
2014-03-06 10:37:25 -05:00
|
|
|
createSesClient = (settings) ->
|
|
|
|
if settings? and settings.key? and settings.key != "" and settings.secret? and settings.secret != ""
|
|
|
|
client = nodemailer.createTransport("SES", {AWSAccessKeyID: settings.key, AWSSecretKey: settings.secret} )
|
|
|
|
else
|
|
|
|
logger.warn "AWS SES credentials are not configured. No emails will be sent."
|
2014-03-04 13:10:06 -05:00
|
|
|
|
2014-03-06 10:37:25 -05:00
|
|
|
if Settings.email?
|
|
|
|
switch Settings.email.transport
|
|
|
|
when "ses"
|
|
|
|
createSesClient( Settings.email.ses)
|
|
|
|
# TODO direct, client
|
|
|
|
when undefined,null,""
|
|
|
|
logger.warn "No Email transport defined. No emails will be sent."
|
|
|
|
else
|
|
|
|
logger.warn "Uknown email transport #{Settings.email.transport}. No emails will be sent."
|
|
|
|
|
|
|
|
module.exports =
|
2014-03-04 13:10:06 -05:00
|
|
|
sendEmail : (options, callback = (error) ->)->
|
|
|
|
logger.log receiver:options.receiver, subject:options.subject, "sending email"
|
|
|
|
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
|
|
|
|
message: options.html
|
2014-03-05 07:06:28 -05:00
|
|
|
replyTo: options.replyTo || Settings.email.replyToAddress
|
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)
|
2014-03-06 10:37:25 -05:00
|
|
|
|