EmailSender.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const { callbackify } = require('util')
  2. const logger = require('logger-sharelatex')
  3. const metrics = require('metrics-sharelatex')
  4. const Settings = require('settings-sharelatex')
  5. const nodemailer = require('nodemailer')
  6. const sesTransport = require('nodemailer-ses-transport')
  7. const mandrillTransport = require('nodemailer-mandrill-transport')
  8. const OError = require('@overleaf/o-error')
  9. const RateLimiter = require('../../infrastructure/RateLimiter')
  10. const _ = require('underscore')
  11. const EMAIL_SETTINGS = Settings.email || {}
  12. module.exports = {
  13. sendEmail: callbackify(sendEmail),
  14. promises: {
  15. sendEmail
  16. }
  17. }
  18. const client = getClient()
  19. function getClient() {
  20. let client
  21. if (EMAIL_SETTINGS.parameters) {
  22. const emailParameters = EMAIL_SETTINGS.parameters
  23. if (emailParameters.AWSAccessKeyID || EMAIL_SETTINGS.driver === 'ses') {
  24. logger.log('using aws ses for email')
  25. client = nodemailer.createTransport(sesTransport(emailParameters))
  26. } else if (emailParameters.sendgridApiKey) {
  27. throw new OError(
  28. 'sendgridApiKey configuration option is deprecated, use SMTP instead'
  29. )
  30. } else if (emailParameters.MandrillApiKey) {
  31. logger.log('using mandril for email')
  32. client = nodemailer.createTransport(
  33. mandrillTransport({
  34. auth: {
  35. apiKey: emailParameters.MandrillApiKey
  36. }
  37. })
  38. )
  39. } else {
  40. logger.log('using smtp for email')
  41. const smtp = _.pick(
  42. emailParameters,
  43. 'host',
  44. 'port',
  45. 'secure',
  46. 'auth',
  47. 'ignoreTLS'
  48. )
  49. client = nodemailer.createTransport(smtp)
  50. }
  51. } else {
  52. logger.warn(
  53. 'Email transport and/or parameters not defined. No emails will be sent.'
  54. )
  55. client = {
  56. async sendMail(options) {
  57. logger.log({ options }, 'Would send email if enabled.')
  58. }
  59. }
  60. }
  61. return client
  62. }
  63. async function sendEmail(options) {
  64. try {
  65. const canContinue = await checkCanSendEmail(options)
  66. if (!canContinue) {
  67. logger.log(
  68. {
  69. sendingUser_id: options.sendingUser_id,
  70. to: options.to,
  71. subject: options.subject,
  72. canContinue
  73. },
  74. 'rate limit hit for sending email, not sending'
  75. )
  76. throw new OError('rate limit hit sending email')
  77. }
  78. metrics.inc('email')
  79. let sendMailOptions = {
  80. to: options.to,
  81. from: EMAIL_SETTINGS.fromAddress || '',
  82. subject: options.subject,
  83. html: options.html,
  84. text: options.text,
  85. replyTo: options.replyTo || EMAIL_SETTINGS.replyToAddress,
  86. socketTimeout: 30 * 1000
  87. }
  88. if (EMAIL_SETTINGS.textEncoding != null) {
  89. sendMailOptions.textEncoding = EMAIL_SETTINGS.textEncoding
  90. }
  91. await client.sendMail(sendMailOptions)
  92. } catch (err) {
  93. throw new OError('error sending message').withCause(err)
  94. }
  95. }
  96. async function checkCanSendEmail(options) {
  97. if (options.sendingUser_id == null) {
  98. // email not sent from user, not rate limited
  99. return true
  100. }
  101. const opts = {
  102. endpointName: 'send_email',
  103. timeInterval: 60 * 60 * 3,
  104. subjectName: options.sendingUser_id,
  105. throttle: 100
  106. }
  107. const allowed = await RateLimiter.promises.addCount(opts)
  108. return allowed
  109. }