EmailSender.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. const { callbackify } = require('util')
  2. const logger = require('@overleaf/logger')
  3. const metrics = require('@overleaf/metrics')
  4. const Settings = require('@overleaf/settings')
  5. const nodemailer = require('nodemailer')
  6. const sesTransport = require('nodemailer-ses-transport')
  7. const OError = require('@overleaf/o-error')
  8. const { RateLimiter } = require('../../infrastructure/RateLimiter')
  9. const _ = require('lodash')
  10. const EMAIL_SETTINGS = Settings.email || {}
  11. module.exports = {
  12. sendEmail: callbackify(sendEmail),
  13. promises: {
  14. sendEmail,
  15. },
  16. }
  17. const client = getClient()
  18. const rateLimiter = new RateLimiter('send_email', {
  19. points: 100,
  20. duration: 3 * 60 * 60,
  21. })
  22. function getClient() {
  23. let client
  24. if (EMAIL_SETTINGS.parameters) {
  25. const emailParameters = EMAIL_SETTINGS.parameters
  26. if (emailParameters.AWSAccessKeyID || EMAIL_SETTINGS.driver === 'ses') {
  27. logger.debug('using aws ses for email')
  28. client = nodemailer.createTransport(sesTransport(emailParameters))
  29. } else if (emailParameters.sendgridApiKey) {
  30. throw new OError(
  31. 'sendgridApiKey configuration option is deprecated, use SMTP instead'
  32. )
  33. } else if (emailParameters.MandrillApiKey) {
  34. throw new OError(
  35. 'MandrillApiKey configuration option is deprecated, use SMTP instead'
  36. )
  37. } else {
  38. logger.debug('using smtp for email')
  39. const smtp = _.pick(
  40. emailParameters,
  41. 'host',
  42. 'port',
  43. 'secure',
  44. 'auth',
  45. 'ignoreTLS',
  46. 'tls',
  47. 'logger',
  48. 'name'
  49. )
  50. client = nodemailer.createTransport(smtp)
  51. }
  52. } else {
  53. logger.warn(
  54. 'Email transport and/or parameters not defined. No emails will be sent.'
  55. )
  56. client = {
  57. async sendMail(options) {
  58. logger.info({ options }, 'Would send email if enabled.')
  59. },
  60. }
  61. }
  62. return client
  63. }
  64. async function sendEmail(options, emailType) {
  65. try {
  66. const canContinue = await checkCanSendEmail(options)
  67. metrics.inc('email_status', {
  68. status: canContinue ? 'sent' : 'rate_limited',
  69. path: emailType,
  70. })
  71. if (!canContinue) {
  72. logger.debug(
  73. {
  74. sendingUserId: options.sendingUser_id,
  75. to: options.to,
  76. subject: options.subject,
  77. canContinue,
  78. },
  79. 'rate limit hit for sending email, not sending'
  80. )
  81. throw new OError('rate limit hit sending email')
  82. }
  83. metrics.inc('email')
  84. const sendMailOptions = {
  85. to: options.to,
  86. from: EMAIL_SETTINGS.fromAddress || '',
  87. subject: options.subject,
  88. html: options.html,
  89. text: options.text,
  90. replyTo: options.replyTo || EMAIL_SETTINGS.replyToAddress,
  91. socketTimeout: 30 * 1000,
  92. }
  93. if (EMAIL_SETTINGS.textEncoding != null) {
  94. sendMailOptions.textEncoding = EMAIL_SETTINGS.textEncoding
  95. }
  96. if (options.category) {
  97. // category support for sendgrid
  98. sendMailOptions.headers = {
  99. 'X-SMTPAPI': JSON.stringify({ category: options.category }),
  100. }
  101. }
  102. await client.sendMail(sendMailOptions)
  103. } catch (err) {
  104. throw new OError('error sending message').withCause(err)
  105. }
  106. }
  107. async function checkCanSendEmail(options) {
  108. if (options.sendingUser_id == null) {
  109. // email not sent from user, not rate limited
  110. return true
  111. }
  112. try {
  113. await rateLimiter.consume(options.sendingUser_id, 1, { method: 'userId' })
  114. } catch (err) {
  115. if (err instanceof Error) {
  116. throw err
  117. } else {
  118. return false
  119. }
  120. }
  121. return true
  122. }