overleaf/services/web/app/coffee/Features/Email/EmailSender.coffee

46 lines
1.5 KiB
CoffeeScript
Raw Normal View History

logger = require('logger-sharelatex')
metrics = require('../../infrastructure/Metrics')
Settings = require('settings-sharelatex')
metrics = require("../../infrastructure/Metrics")
2014-03-06 15:37:25 +00:00
nodemailer = require("nodemailer")
if Settings.email? and Settings.email.fromAddress?
defaultFromAddress = Settings.email.fromAddress
2014-03-06 15:37:25 +00:00
else
defaultFromAddress = ""
2014-03-06 15:37:25 +00: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-06 15:37:25 +00:00
if Settings.email?
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 15:37:25 +00:00
else
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-06 15:37:25 +00:00
module.exports =
sendEmail : (options, callback = (error) ->)->
2014-03-12 23:37:54 +00:00
logger.log receiver:options.to, subject:options.subject, "sending email"
metrics.inc "email"
2014-03-06 15:37:25 +00:00
options =
to: options.to
from: defaultFromAddress
subject: options.subject
html: options.html
replyTo: options.replyTo || Settings.email.replyToAddress
socketTimeout: 30 * 1000
2014-03-06 15:37:25 +00:00
client.sendMail options, (err, res)->
if err?
logger.err err:err, "error sending message"
else
logger.log "Message sent to #{options.to}"
callback(err)
2014-03-06 15:37:25 +00:00