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

63 lines
2 KiB
CoffeeScript
Raw Normal View History

logger = require('logger-sharelatex')
metrics = require('../../infrastructure/Metrics')
Settings = require('settings-sharelatex')
2014-03-06 10:37:25 -05:00
nodemailer = require("nodemailer")
sesTransport = require('nodemailer-ses-transport')
2016-03-29 09:15:33 -04:00
sgTransport = require('nodemailer-sendgrid-transport')
_ = require("underscore")
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()
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}}))
else if Settings?.email?.parameters?
2016-11-02 13:14:54 -04:00
smtp = _.pick(Settings?.email?.parameters, "host", "port", "secure", "auth", "ignoreTLS")
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-06 10:37:25 -05:00
module.exports =
sendEmail : (options, callback = (error) ->)->
2014-03-12 19:37:54 -04:00
logger.log receiver:options.to, 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
html: options.html
text: options.text
replyTo: options.replyTo || Settings.email.replyToAddress
socketTimeout: 30 * 1000
if Settings.email.textEncoding?
2016-10-13 09:21:54 -04:00
opts.textEncoding = textEncoding
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)