EmailSender.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. const { callbackify } = require('util')
  2. const logger = require('logger-sharelatex')
  3. const metrics = require('@overleaf/metrics')
  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. 'logger',
  49. 'name'
  50. )
  51. client = nodemailer.createTransport(smtp)
  52. }
  53. } else {
  54. logger.warn(
  55. 'Email transport and/or parameters not defined. No emails will be sent.'
  56. )
  57. client = {
  58. async sendMail(options) {
  59. logger.log({ options }, 'Would send email if enabled.')
  60. },
  61. }
  62. }
  63. return client
  64. }
  65. async function sendEmail(options) {
  66. try {
  67. const canContinue = await checkCanSendEmail(options)
  68. if (!canContinue) {
  69. logger.log(
  70. {
  71. sendingUser_id: options.sendingUser_id,
  72. to: options.to,
  73. subject: options.subject,
  74. canContinue,
  75. },
  76. 'rate limit hit for sending email, not sending'
  77. )
  78. throw new OError('rate limit hit sending email')
  79. }
  80. metrics.inc('email')
  81. let sendMailOptions = {
  82. to: options.to,
  83. from: EMAIL_SETTINGS.fromAddress || '',
  84. subject: options.subject,
  85. html: options.html,
  86. text: options.text,
  87. replyTo: options.replyTo || EMAIL_SETTINGS.replyToAddress,
  88. socketTimeout: 30 * 1000,
  89. }
  90. if (EMAIL_SETTINGS.textEncoding != null) {
  91. sendMailOptions.textEncoding = EMAIL_SETTINGS.textEncoding
  92. }
  93. await client.sendMail(sendMailOptions)
  94. } catch (err) {
  95. throw new OError('error sending message').withCause(err)
  96. }
  97. }
  98. async function checkCanSendEmail(options) {
  99. if (options.sendingUser_id == null) {
  100. // email not sent from user, not rate limited
  101. return true
  102. }
  103. const opts = {
  104. endpointName: 'send_email',
  105. timeInterval: 60 * 60 * 3,
  106. subjectName: options.sendingUser_id,
  107. throttle: 100,
  108. }
  109. const allowed = await RateLimiter.promises.addCount(opts)
  110. return allowed
  111. }