EmailSender.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. 'logger',
  47. 'name'
  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.info({ options }, 'Would send email if enabled.')
  58. },
  59. }
  60. }
  61. return client
  62. }
  63. async function sendEmail(options, emailType) {
  64. try {
  65. const canContinue = await checkCanSendEmail(options)
  66. metrics.inc('email_status', {
  67. status: canContinue ? 'sent' : 'rate_limited',
  68. path: emailType,
  69. })
  70. if (!canContinue) {
  71. logger.debug(
  72. {
  73. sendingUserId: options.sendingUser_id,
  74. to: options.to,
  75. subject: options.subject,
  76. canContinue,
  77. },
  78. 'rate limit hit for sending email, not sending'
  79. )
  80. throw new OError('rate limit hit sending email')
  81. }
  82. metrics.inc('email')
  83. const sendMailOptions = {
  84. to: options.to,
  85. from: EMAIL_SETTINGS.fromAddress || '',
  86. subject: options.subject,
  87. html: options.html,
  88. text: options.text,
  89. replyTo: options.replyTo || EMAIL_SETTINGS.replyToAddress,
  90. socketTimeout: 30 * 1000,
  91. }
  92. if (EMAIL_SETTINGS.textEncoding != null) {
  93. sendMailOptions.textEncoding = EMAIL_SETTINGS.textEncoding
  94. }
  95. if (options.category) {
  96. // category support for sendgrid
  97. sendMailOptions.headers = {
  98. 'X-SMTPAPI': JSON.stringify({ category: options.category }),
  99. }
  100. }
  101. await client.sendMail(sendMailOptions)
  102. } catch (err) {
  103. throw new OError('error sending message').withCause(err)
  104. }
  105. }
  106. async function checkCanSendEmail(options) {
  107. if (options.sendingUser_id == null) {
  108. // email not sent from user, not rate limited
  109. return true
  110. }
  111. try {
  112. await rateLimiter.consume(options.sendingUser_id, 1, { method: 'userId' })
  113. } catch (err) {
  114. if (err instanceof Error) {
  115. throw err
  116. } else {
  117. return false
  118. }
  119. }
  120. return true
  121. }