2014-03-04 13:10:06 -05:00
logger = require ( ' logger-sharelatex ' )
2017-04-03 11:18:30 -04:00
metrics = require ( ' metrics-sharelatex ' )
2014-03-04 13:10:06 -05:00
Settings = require ( ' settings-sharelatex ' )
2014-03-06 10:37:25 -05:00
nodemailer = require ( " nodemailer " )
2016-02-12 12:13:45 -05:00
sesTransport = require ( ' nodemailer-ses-transport ' )
2016-03-29 09:15:33 -04:00
sgTransport = require ( ' nodemailer-sendgrid-transport ' )
2017-12-11 06:05:04 -05:00
mandrillTransport = require ( ' nodemailer-mandrill-transport ' )
2017-01-14 09:52:32 -05:00
rateLimiter = require ( ' ../../infrastructure/RateLimiter ' )
2016-02-12 12:13:45 -05:00
_ = require ( " underscore " )
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 ( )
2017-06-21 05:57:13 -04:00
if Settings ? . email ? . parameters ? . AWSAccessKeyID ? or Settings ? . email ? . driver == ' ses '
2016-02-12 12:13:45 -05:00
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 } } ) )
2017-12-11 06:48:31 -05:00
else if Settings ? . email ? . parameters ? . MandrillApiKey ?
2017-12-11 06:05:04 -05:00
logger . log " using mandril for email "
2017-12-11 06:48:31 -05:00
nm_client = nodemailer . createTransport ( mandrillTransport ( { auth : { apiKey : Settings ? . email ? . parameters ? . MandrillApiKey } } ) )
2016-02-12 12:13:45 -05:00
else if Settings ? . email ? . parameters ?
logger . log " using smtp for email "
2017-12-11 06:05:04 -05:00
smtp = _ . pick ( Settings ? . email ? . parameters , " host " , " port " , " secure " , " auth " , " ignoreTLS " )
2016-02-12 12:13:45 -05:00
nm_client = nodemailer . createTransport ( smtp )
else
logger . warn " Email transport and/or parameters not defined. No emails will be sent. "
2017-12-11 06:05:04 -05:00
nm_client = client
2016-02-12 12:13:45 -05:00
if nm_client ?
client = nm_client
else
logger . warn " Failed to create email transport. Please check your settings. No email will be sent. "
2017-01-14 09:52:32 -05:00
checkCanSendEmail = (options, callback)->
if ! options . sendingUser_id ? #email not sent from user, not rate limited
2017-01-17 11:25:18 -05:00
return callback ( null , true )
2017-01-14 09:52:32 -05:00
opts =
endpointName: " send_email "
timeInterval: 60 * 60 * 3
subjectName: options . sendingUser_id
throttle: 100
rateLimiter . addCount opts , callback
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 "
2017-01-14 09:52:32 -05:00
checkCanSendEmail options , (err, canContinue)->
2014-03-04 13:10:06 -05:00
if err ?
2017-01-14 09:52:32 -05:00
return callback ( err )
if ! canContinue
logger . log sendingUser_id : options . sendingUser_id , to : options . to , subject : options . subject , canContinue : canContinue , " rate limit hit for sending email, not sending "
return callback ( " rate limit hit sending email " )
metrics . inc " email "
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 ?
opts.textEncoding = textEncoding
client . sendMail options , (err, res)->
if err ?
logger . err err : err , " error sending message "
2017-03-27 12:45:19 -04:00
err = new Error ( ' Cannot send email ' )
2017-01-14 09:52:32 -05:00
else
logger . log " Message sent to #{ options . to } "
2017-03-27 12:45:19 -04:00
callback ( err )