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
|
|
|
if Settings.email?
|
2014-03-12 07:43:36 -04:00
|
|
|
if Settings.email.transport? and Settings.email.parameters?
|
|
|
|
nm_client = nodemailer.createTransport( Settings.email.transport, Settings.email.parameters )
|
|
|
|
if nm_client
|
|
|
|
client = nm_client
|
2014-03-06 10:37:25 -05:00
|
|
|
else
|
2014-03-12 07:43:36 -04:00
|
|
|
logger.warn "Failed to create email transport. Please check your settings. No email will be sent."
|
|
|
|
else
|
|
|
|
logger.warn "Email transport and/or parameters not defined. No emails 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)
|
2014-03-06 10:37:25 -05:00
|
|
|
|