|
|
@@ -4,7 +4,7 @@ Settings = require('settings-sharelatex')
|
|
|
nodemailer = require("nodemailer")
|
|
|
sesTransport = require('nodemailer-ses-transport')
|
|
|
sgTransport = require('nodemailer-sendgrid-transport')
|
|
|
-
|
|
|
+rateLimiter = require('../../infrastructure/RateLimiter')
|
|
|
_ = require("underscore")
|
|
|
|
|
|
if Settings.email? and Settings.email.fromAddress?
|
|
|
@@ -39,24 +39,39 @@ if nm_client?
|
|
|
else
|
|
|
logger.warn "Failed to create email transport. Please check your settings. No email will be sent."
|
|
|
|
|
|
+checkCanSendEmail = (options, callback)->
|
|
|
+ if !options.sendingUser_id? #email not sent from user, not rate limited
|
|
|
+ callback(null, true)
|
|
|
+ opts =
|
|
|
+ endpointName: "send_email"
|
|
|
+ timeInterval: 60 * 60 * 3
|
|
|
+ subjectName: options.sendingUser_id
|
|
|
+ throttle: 100
|
|
|
+ rateLimiter.addCount opts, callback
|
|
|
|
|
|
module.exports =
|
|
|
sendEmail : (options, callback = (error) ->)->
|
|
|
logger.log receiver:options.to, subject:options.subject, "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)->
|
|
|
+ checkCanSendEmail options, (err, canContinue)->
|
|
|
if err?
|
|
|
- logger.err err:err, "error sending message"
|
|
|
- else
|
|
|
- logger.log "Message sent to #{options.to}"
|
|
|
- callback(err)
|
|
|
+ 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"
|
|
|
+ else
|
|
|
+ logger.log "Message sent to #{options.to}"
|
|
|
+ callback(err)
|