EmailBuilder.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. const _ = require('underscore')
  2. const settings = require('settings-sharelatex')
  3. const moment = require('moment')
  4. const EmailMessageHelper = require('./EmailMessageHelper')
  5. const StringHelper = require('../Helpers/StringHelper')
  6. const BaseWithHeaderEmailLayout = require('./Layouts/BaseWithHeaderEmailLayout')
  7. const SpamSafe = require('./SpamSafe')
  8. const ctaEmailBody = require('./Bodies/cta-email')
  9. const NoCTAEmailBody = require('./Bodies/NoCTAEmailBody')
  10. function _emailBodyPlainText(content, opts, ctaEmail) {
  11. let emailBody = `${content.greeting(opts, true)}`
  12. emailBody += `\r\n\r\n`
  13. emailBody += `${content.message(opts, true).join('\r\n\r\n')}`
  14. if (ctaEmail) {
  15. emailBody += `\r\n\r\n`
  16. emailBody += `${content.ctaText(opts, true)}: ${content.ctaURL(opts, true)}`
  17. }
  18. if (
  19. content.secondaryMessage(opts, true) &&
  20. content.secondaryMessage(opts, true).length > 0
  21. ) {
  22. emailBody += `\r\n\r\n`
  23. emailBody += `${content.secondaryMessage(opts, true).join('\r\n\r\n')}`
  24. }
  25. emailBody += `\r\n\r\n`
  26. emailBody += `Regards,\r\nThe ${settings.appName} Team - ${settings.siteUrl}`
  27. if (
  28. settings.email &&
  29. settings.email.template &&
  30. settings.email.template.customFooter
  31. ) {
  32. emailBody += `\r\n\r\n`
  33. emailBody += settings.email.template.customFooter
  34. }
  35. return emailBody
  36. }
  37. function ctaTemplate(content) {
  38. if (
  39. !content.ctaURL ||
  40. !content.ctaText ||
  41. !content.message ||
  42. !content.subject
  43. ) {
  44. throw new Error('missing required CTA email content')
  45. }
  46. if (!content.title) {
  47. content.title = () => {}
  48. }
  49. if (!content.greeting) {
  50. content.greeting = () => 'Hi,'
  51. }
  52. if (!content.secondaryMessage) {
  53. content.secondaryMessage = () => []
  54. }
  55. if (!content.gmailGoToAction) {
  56. content.gmailGoToAction = () => {}
  57. }
  58. return {
  59. subject(opts) {
  60. return content.subject(opts)
  61. },
  62. layout: BaseWithHeaderEmailLayout,
  63. plainTextTemplate(opts) {
  64. return _emailBodyPlainText(content, opts, true)
  65. },
  66. compiledTemplate(opts) {
  67. return ctaEmailBody({
  68. title: content.title(opts),
  69. greeting: content.greeting(opts),
  70. message: content.message(opts),
  71. secondaryMessage: content.secondaryMessage(opts),
  72. ctaText: content.ctaText(opts),
  73. ctaURL: content.ctaURL(opts),
  74. gmailGoToAction: content.gmailGoToAction(opts),
  75. StringHelper
  76. })
  77. }
  78. }
  79. }
  80. function NoCTAEmailTemplate(content) {
  81. if (content.greeting == null) {
  82. content.greeting = () => 'Hi,'
  83. }
  84. if (!content.message) {
  85. throw new Error('missing message')
  86. }
  87. return {
  88. subject(opts) {
  89. return content.subject(opts)
  90. },
  91. layout: BaseWithHeaderEmailLayout,
  92. plainTextTemplate(opts) {
  93. return `\
  94. ${content.greeting(opts)}
  95. ${content.message(opts, true).join('\r\n\r\n')}
  96. Regards,
  97. The ${settings.appName} Team - ${settings.siteUrl}\
  98. `
  99. },
  100. compiledTemplate(opts) {
  101. return NoCTAEmailBody({
  102. title:
  103. typeof content.title === 'function' ? content.title(opts) : undefined,
  104. greeting: content.greeting(opts),
  105. message: content.message(opts),
  106. StringHelper
  107. })
  108. }
  109. }
  110. }
  111. function buildEmail(templateName, opts) {
  112. const template = templates[templateName]
  113. opts.siteUrl = settings.siteUrl
  114. opts.body = template.compiledTemplate(opts)
  115. return {
  116. subject: template.subject(opts),
  117. html: template.layout(opts),
  118. text: template.plainTextTemplate && template.plainTextTemplate(opts)
  119. }
  120. }
  121. const templates = {}
  122. templates.registered = ctaTemplate({
  123. subject() {
  124. return `Activate your ${settings.appName} Account`
  125. },
  126. message(opts) {
  127. return [
  128. `Congratulations, you've just had an account created for you on ${
  129. settings.appName
  130. } with the email address '${_.escape(opts.to)}'.`,
  131. 'Click here to set your password and log in:'
  132. ]
  133. },
  134. secondaryMessage() {
  135. return [
  136. `If you have any questions or problems, please contact ${settings.adminEmail}`
  137. ]
  138. },
  139. ctaText() {
  140. return 'Set password'
  141. },
  142. ctaURL(opts) {
  143. return opts.setNewPasswordUrl
  144. }
  145. })
  146. templates.canceledSubscription = ctaTemplate({
  147. subject() {
  148. return `${settings.appName} thoughts`
  149. },
  150. message() {
  151. return [
  152. `We are sorry to see you cancelled your ${settings.appName} premium subscription. Would you mind giving us some feedback on what the site is lacking at the moment via this quick survey?`
  153. ]
  154. },
  155. secondaryMessage() {
  156. return ['Thank you in advance!']
  157. },
  158. ctaText() {
  159. return 'Leave Feedback'
  160. },
  161. ctaURL(opts) {
  162. return 'https://docs.google.com/forms/d/e/1FAIpQLSfa7z_s-cucRRXm70N4jEcSbFsZeb0yuKThHGQL8ySEaQzF0Q/viewform?usp=sf_link'
  163. }
  164. })
  165. templates.reactivatedSubscription = ctaTemplate({
  166. subject() {
  167. return `Subscription Reactivated - ${settings.appName}`
  168. },
  169. message(opts) {
  170. return ['Your subscription was reactivated successfully.']
  171. },
  172. ctaText() {
  173. return 'View Subscription Dashboard'
  174. },
  175. ctaURL(opts) {
  176. return `${settings.siteUrl}/user/subscription`
  177. }
  178. })
  179. templates.passwordResetRequested = ctaTemplate({
  180. subject() {
  181. return `Password Reset - ${settings.appName}`
  182. },
  183. title() {
  184. return 'Password Reset'
  185. },
  186. message() {
  187. return [`We got a request to reset your ${settings.appName} password.`]
  188. },
  189. secondaryMessage() {
  190. return [
  191. "If you ignore this message, your password won't be changed.",
  192. "If you didn't request a password reset, let us know."
  193. ]
  194. },
  195. ctaText() {
  196. return 'Reset password'
  197. },
  198. ctaURL(opts) {
  199. return opts.setNewPasswordUrl
  200. }
  201. })
  202. templates.confirmEmail = ctaTemplate({
  203. subject() {
  204. return `Confirm Email - ${settings.appName}`
  205. },
  206. title() {
  207. return 'Confirm Email'
  208. },
  209. message(opts) {
  210. return [
  211. `Please confirm that you have added a new email, ${opts.to}, to your ${
  212. settings.appName
  213. } account.`
  214. ]
  215. },
  216. secondaryMessage() {
  217. return [
  218. 'If you did not request this, you can simply ignore this message.',
  219. `If you have any questions or trouble confirming your email address, please get in touch with our support team at ${
  220. settings.adminEmail
  221. }.`
  222. ]
  223. },
  224. ctaText() {
  225. return 'Confirm Email'
  226. },
  227. ctaURL(opts) {
  228. return opts.confirmEmailUrl
  229. }
  230. })
  231. templates.projectInvite = ctaTemplate({
  232. subject(opts) {
  233. return `${_.escape(
  234. SpamSafe.safeProjectName(opts.project.name, 'New Project')
  235. )} - shared by ${_.escape(
  236. SpamSafe.safeEmail(opts.owner.email, 'a collaborator')
  237. )}`
  238. },
  239. title(opts) {
  240. return `${_.escape(
  241. SpamSafe.safeProjectName(opts.project.name, 'New Project')
  242. )} - shared by ${_.escape(
  243. SpamSafe.safeEmail(opts.owner.email, 'a collaborator')
  244. )}`
  245. },
  246. message(opts) {
  247. return [
  248. `${_.escape(
  249. SpamSafe.safeEmail(opts.owner.email, 'a collaborator')
  250. )} wants to share ${_.escape(
  251. SpamSafe.safeProjectName(opts.project.name, 'a new project')
  252. )} with you.`
  253. ]
  254. },
  255. ctaText() {
  256. return 'View project'
  257. },
  258. ctaURL(opts) {
  259. return opts.inviteUrl
  260. },
  261. gmailGoToAction(opts) {
  262. return {
  263. target: opts.inviteUrl,
  264. name: 'View project',
  265. description: `Join ${_.escape(
  266. SpamSafe.safeProjectName(opts.project.name, 'project')
  267. )} at ${settings.appName}`
  268. }
  269. }
  270. })
  271. templates.verifyEmailToJoinTeam = ctaTemplate({
  272. subject(opts) {
  273. return `${_.escape(
  274. _formatUserNameAndEmail(opts.inviter, 'A collaborator')
  275. )} has invited you to join a team on ${settings.appName}`
  276. },
  277. title(opts) {
  278. return `${_.escape(
  279. _formatUserNameAndEmail(opts.inviter, 'A collaborator')
  280. )} has invited you to join a team on ${settings.appName}`
  281. },
  282. message(opts) {
  283. return [
  284. `Please click the button below to join the team and enjoy the benefits of an upgraded ${settings.appName} account.`
  285. ]
  286. },
  287. ctaText(opts) {
  288. return 'Join now'
  289. },
  290. ctaURL(opts) {
  291. return opts.acceptInviteUrl
  292. }
  293. })
  294. templates.testEmail = ctaTemplate({
  295. subject() {
  296. return `A Test Email from ${settings.appName}`
  297. },
  298. title() {
  299. return `A Test Email from ${settings.appName}`
  300. },
  301. greeting() {
  302. return 'Hi,'
  303. },
  304. message() {
  305. return [`This is a test Email from ${settings.appName}`]
  306. },
  307. ctaText() {
  308. return `Open ${settings.appName}`
  309. },
  310. ctaURL() {
  311. return settings.siteUrl
  312. }
  313. })
  314. templates.ownershipTransferConfirmationPreviousOwner = NoCTAEmailTemplate({
  315. subject(opts) {
  316. return `Project ownership transfer - ${settings.appName}`
  317. },
  318. title(opts) {
  319. const projectName = _.escape(
  320. SpamSafe.safeProjectName(opts.project.name, 'Your project')
  321. )
  322. return `${projectName} - Owner change`
  323. },
  324. message(opts, isPlainText) {
  325. const nameAndEmail = _.escape(
  326. _formatUserNameAndEmail(opts.newOwner, 'a collaborator')
  327. )
  328. const projectName = _.escape(
  329. SpamSafe.safeProjectName(opts.project.name, 'your project')
  330. )
  331. const projectNameDisplay = isPlainText
  332. ? projectName
  333. : `<b>${projectName}</b>`
  334. return [
  335. `As per your request, we have made ${nameAndEmail} the owner of ${projectNameDisplay}.`,
  336. `If you haven't asked to change the owner of ${projectNameDisplay}, please get in touch with us via ${settings.adminEmail}.`
  337. ]
  338. }
  339. })
  340. templates.ownershipTransferConfirmationNewOwner = ctaTemplate({
  341. subject(opts) {
  342. return `Project ownership transfer - ${settings.appName}`
  343. },
  344. title(opts) {
  345. const projectName = _.escape(
  346. SpamSafe.safeProjectName(opts.project.name, 'Your project')
  347. )
  348. return `${projectName} - Owner change`
  349. },
  350. message(opts, isPlainText) {
  351. const nameAndEmail = _.escape(
  352. _formatUserNameAndEmail(opts.previousOwner, 'A collaborator')
  353. )
  354. const projectName = _.escape(
  355. SpamSafe.safeProjectName(opts.project.name, 'a project')
  356. )
  357. const projectNameEmphasized = isPlainText
  358. ? projectName
  359. : `<b>${projectName}</b>`
  360. return [
  361. `${nameAndEmail} has made you the owner of ${projectNameEmphasized}. You can now manage ${projectName} sharing settings.`
  362. ]
  363. },
  364. ctaText(opts) {
  365. return 'View project'
  366. },
  367. ctaURL(opts) {
  368. const projectUrl = `${
  369. settings.siteUrl
  370. }/project/${opts.project._id.toString()}`
  371. return projectUrl
  372. }
  373. })
  374. templates.userOnboardingEmail = NoCTAEmailTemplate({
  375. subject(opts) {
  376. return `Getting more out of ${settings.appName}`
  377. },
  378. greeting(opts) {
  379. return ''
  380. },
  381. title(opts) {
  382. return `Getting more out of ${settings.appName}`
  383. },
  384. message(opts, isPlainText) {
  385. const learnLatexLink = EmailMessageHelper.displayLink(
  386. 'Learn LaTeX in 30 minutes',
  387. `${settings.siteUrl}/learn/latex/Learn_LaTeX_in_30_minutes?utm_source=overleaf&utm_medium=email&utm_campaign=onboarding`,
  388. isPlainText
  389. )
  390. const templatesLinks = EmailMessageHelper.displayLink(
  391. 'Find a beautiful template',
  392. `${settings.siteUrl}/latex/templates?utm_source=overleaf&utm_medium=email&utm_campaign=onboarding`,
  393. isPlainText
  394. )
  395. const collaboratorsLink = EmailMessageHelper.displayLink(
  396. 'Work with your collaborators',
  397. `${settings.siteUrl}/learn/how-to/Sharing_a_project?utm_source=overleaf&utm_medium=email&utm_campaign=onboarding`,
  398. isPlainText
  399. )
  400. const siteLink = EmailMessageHelper.displayLink(
  401. 'www.overleaf.com',
  402. settings.siteUrl,
  403. isPlainText
  404. )
  405. const userSettingsLink = EmailMessageHelper.displayLink(
  406. 'here',
  407. `${settings.siteUrl}/user/settings`,
  408. isPlainText
  409. )
  410. const onboardingSurveyLink = EmailMessageHelper.displayLink(
  411. 'Join our user feedback programme',
  412. 'https://forms.gle/DB7pdk2B1VFQqVVB9',
  413. isPlainText
  414. )
  415. return [
  416. `Thanks for signing up for ${settings.appName} recently. We hope you've been finding it useful! Here are some key features to help you get the most out of the service:`,
  417. `${learnLatexLink}: In this tutorial we provide a quick and easy first introduction to LaTeX with no prior knowledge required. By the time you are finished, you will have written your first LaTeX document!`,
  418. `${templatesLinks}: If you're looking for a template or example to get started, we've a large selection available in our template gallery, including CVs, project reports, journal articles and more.`,
  419. `${collaboratorsLink}: One of the key features of Overleaf is the ability to share projects and collaborate on them with other users. Find out how to share your projecs with your colleagues in this quick how-to guide.`,
  420. `${onboardingSurveyLink} to help us make Overleaf even better!`,
  421. 'Thanks again for using Overleaf :)',
  422. `John`,
  423. `Dr John Hammersley <br />Co-founder & CEO <br />${siteLink}<hr>`,
  424. `Don't want onboarding emails like this from us? Don't worry, this is the only one. If you've previously subscribed to emails about product offers and company news and events, you can unsubscribe ${userSettingsLink}.`
  425. ]
  426. }
  427. })
  428. templates.securityAlert = NoCTAEmailTemplate({
  429. subject(opts) {
  430. return `Overleaf security note: ${opts.action}`
  431. },
  432. title(opts) {
  433. return opts.action.charAt(0).toUpperCase() + opts.action.slice(1)
  434. },
  435. message(opts, isPlainText) {
  436. const dateFormatted = moment().format('dddd D MMMM YYYY')
  437. const timeFormatted = moment().format('HH:mm')
  438. const helpLink = EmailMessageHelper.displayLink(
  439. 'quick guide',
  440. `${settings.siteUrl}/learn/how-to/Keeping_your_account_secure`,
  441. isPlainText
  442. )
  443. const actionDescribed = EmailMessageHelper.cleanHTML(
  444. opts.actionDescribed,
  445. isPlainText
  446. )
  447. if (!opts.message) {
  448. opts.message = []
  449. }
  450. const message = opts.message.map(m => {
  451. return EmailMessageHelper.cleanHTML(m, isPlainText)
  452. })
  453. return [
  454. `We are writing to let you know that ${actionDescribed} on ${dateFormatted} at ${timeFormatted} GMT.`,
  455. ...message,
  456. `If this was you, you can ignore this email.`,
  457. `If this was not you, we recommend getting in touch with our support team at ${settings.adminEmail} to report this as potentially suspicious activity on your account.`,
  458. `We also encourage you to read our ${helpLink} to keeping your ${settings.appName} account safe.`
  459. ]
  460. }
  461. })
  462. function _formatUserNameAndEmail(user, placeholder) {
  463. if (user.first_name && user.last_name) {
  464. const fullName = `${user.first_name} ${user.last_name}`
  465. if (SpamSafe.isSafeUserName(fullName)) {
  466. if (SpamSafe.isSafeEmail(user.email)) {
  467. return `${fullName} (${user.email})`
  468. } else {
  469. return fullName
  470. }
  471. }
  472. }
  473. return SpamSafe.safeEmail(user.email, placeholder)
  474. }
  475. module.exports = {
  476. templates,
  477. ctaTemplate,
  478. NoCTAEmailTemplate,
  479. buildEmail
  480. }