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

51 lines
1.7 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 10:37:25 -05:00
nodemailer = require("nodemailer")
if Settings.email? and Settings.email.fromAddress?
defaultFromAddress = Settings.email.fromAddress
2014-03-06 10:37:25 -05:00
else
defaultFromAddress = ""
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-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-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 =
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 =
to: options.to
from: defaultFromAddress
subject: options.subject
message: options.html
replyTo: options.replyTo || Settings.email.replyToAddress
2014-03-06 10:37:25 -05: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 10:37:25 -05:00