EmailBuilder.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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 ${
  137. settings.adminEmail
  138. }`
  139. ]
  140. },
  141. ctaText() {
  142. return 'Set password'
  143. },
  144. ctaURL(opts) {
  145. return opts.setNewPasswordUrl
  146. }
  147. })
  148. templates.canceledSubscription = ctaTemplate({
  149. subject() {
  150. return `${settings.appName} thoughts`
  151. },
  152. message() {
  153. return [
  154. `We are sorry to see you cancelled your ${
  155. settings.appName
  156. } premium subscription. Would you mind giving us some feedback on what the site is lacking at the moment via this quick survey?`
  157. ]
  158. },
  159. secondaryMessage() {
  160. return ['Thank you in advance!']
  161. },
  162. ctaText() {
  163. return 'Leave Feedback'
  164. },
  165. ctaURL(opts) {
  166. return 'https://docs.google.com/forms/d/e/1FAIpQLSfa7z_s-cucRRXm70N4jEcSbFsZeb0yuKThHGQL8ySEaQzF0Q/viewform?usp=sf_link'
  167. }
  168. })
  169. templates.reactivatedSubscription = ctaTemplate({
  170. subject() {
  171. return `Subscription Reactivated - ${settings.appName}`
  172. },
  173. message(opts) {
  174. return ['Your subscription was reactivated successfully.']
  175. },
  176. ctaText() {
  177. return 'View Subscription Dashboard'
  178. },
  179. ctaURL(opts) {
  180. return `${settings.siteUrl}/user/subscription`
  181. }
  182. })
  183. templates.passwordResetRequested = ctaTemplate({
  184. subject() {
  185. return `Password Reset - ${settings.appName}`
  186. },
  187. title() {
  188. return 'Password Reset'
  189. },
  190. message() {
  191. return [`We got a request to reset your ${settings.appName} password.`]
  192. },
  193. secondaryMessage() {
  194. return [
  195. "If you ignore this message, your password won't be changed.",
  196. "If you didn't request a password reset, let us know."
  197. ]
  198. },
  199. ctaText() {
  200. return 'Reset password'
  201. },
  202. ctaURL(opts) {
  203. return opts.setNewPasswordUrl
  204. }
  205. })
  206. templates.confirmEmail = ctaTemplate({
  207. subject() {
  208. return `Confirm Email - ${settings.appName}`
  209. },
  210. title() {
  211. return 'Confirm Email'
  212. },
  213. message() {
  214. return [`Please confirm your email on ${settings.appName}.`]
  215. },
  216. ctaText() {
  217. return 'Confirm Email'
  218. },
  219. ctaURL(opts) {
  220. return opts.confirmEmailUrl
  221. }
  222. })
  223. templates.projectInvite = ctaTemplate({
  224. subject(opts) {
  225. return `${_.escape(
  226. SpamSafe.safeProjectName(opts.project.name, 'New Project')
  227. )} - shared by ${_.escape(
  228. SpamSafe.safeEmail(opts.owner.email, 'a collaborator')
  229. )}`
  230. },
  231. title(opts) {
  232. return `${_.escape(
  233. SpamSafe.safeProjectName(opts.project.name, 'New Project')
  234. )} - shared by ${_.escape(
  235. SpamSafe.safeEmail(opts.owner.email, 'a collaborator')
  236. )}`
  237. },
  238. message(opts) {
  239. return [
  240. `${_.escape(
  241. SpamSafe.safeEmail(opts.owner.email, 'a collaborator')
  242. )} wants to share ${_.escape(
  243. SpamSafe.safeProjectName(opts.project.name, 'a new project')
  244. )} with you.`
  245. ]
  246. },
  247. ctaText() {
  248. return 'View project'
  249. },
  250. ctaURL(opts) {
  251. return opts.inviteUrl
  252. },
  253. gmailGoToAction(opts) {
  254. return {
  255. target: opts.inviteUrl,
  256. name: 'View project',
  257. description: `Join ${_.escape(
  258. SpamSafe.safeProjectName(opts.project.name, 'project')
  259. )} at ${settings.appName}`
  260. }
  261. }
  262. })
  263. templates.verifyEmailToJoinTeam = ctaTemplate({
  264. subject(opts) {
  265. return `${_.escape(
  266. _formatUserNameAndEmail(opts.inviter, 'A collaborator')
  267. )} has invited you to join a team on ${settings.appName}`
  268. },
  269. title(opts) {
  270. return `${_.escape(
  271. _formatUserNameAndEmail(opts.inviter, 'A collaborator')
  272. )} has invited you to join a team on ${settings.appName}`
  273. },
  274. message(opts) {
  275. return [
  276. `Please click the button below to join the team and enjoy the benefits of an upgraded ${
  277. settings.appName
  278. } account.`
  279. ]
  280. },
  281. ctaText(opts) {
  282. return 'Join now'
  283. },
  284. ctaURL(opts) {
  285. return opts.acceptInviteUrl
  286. }
  287. })
  288. templates.testEmail = ctaTemplate({
  289. subject() {
  290. return `A Test Email from ${settings.appName}`
  291. },
  292. title() {
  293. return `A Test Email from ${settings.appName}`
  294. },
  295. greeting() {
  296. return 'Hi,'
  297. },
  298. message() {
  299. return [`This is a test Email from ${settings.appName}`]
  300. },
  301. ctaText() {
  302. return `Open ${settings.appName}`
  303. },
  304. ctaURL() {
  305. return settings.siteUrl
  306. }
  307. })
  308. templates.ownershipTransferConfirmationPreviousOwner = NoCTAEmailTemplate({
  309. subject(opts) {
  310. return `Project ownership transfer - ${settings.appName}`
  311. },
  312. title(opts) {
  313. const projectName = _.escape(
  314. SpamSafe.safeProjectName(opts.project.name, 'Your project')
  315. )
  316. return `${projectName} - Owner change`
  317. },
  318. message(opts, isPlainText) {
  319. const nameAndEmail = _.escape(
  320. _formatUserNameAndEmail(opts.newOwner, 'a collaborator')
  321. )
  322. const projectName = _.escape(
  323. SpamSafe.safeProjectName(opts.project.name, 'your project')
  324. )
  325. const projectNameDisplay = isPlainText
  326. ? projectName
  327. : `<b>${projectName}</b>`
  328. return [
  329. `As per your request, we have made ${nameAndEmail} the owner of ${projectNameDisplay}.`,
  330. `If you haven't asked to change the owner of ${projectNameDisplay}, please get in touch with us via ${
  331. settings.adminEmail
  332. }.`
  333. ]
  334. }
  335. })
  336. templates.ownershipTransferConfirmationNewOwner = ctaTemplate({
  337. subject(opts) {
  338. return `Project ownership transfer - ${settings.appName}`
  339. },
  340. title(opts) {
  341. const projectName = _.escape(
  342. SpamSafe.safeProjectName(opts.project.name, 'Your project')
  343. )
  344. return `${projectName} - Owner change`
  345. },
  346. message(opts, isPlainText) {
  347. const nameAndEmail = _.escape(
  348. _formatUserNameAndEmail(opts.previousOwner, 'A collaborator')
  349. )
  350. const projectName = _.escape(
  351. SpamSafe.safeProjectName(opts.project.name, 'a project')
  352. )
  353. const projectNameEmphasized = isPlainText
  354. ? projectName
  355. : `<b>${projectName}</b>`
  356. return [
  357. `${nameAndEmail} has made you the owner of ${projectNameEmphasized}. You can now manage ${projectName} sharing settings.`
  358. ]
  359. },
  360. ctaText(opts) {
  361. return 'View project'
  362. },
  363. ctaURL(opts) {
  364. const projectUrl = `${
  365. settings.siteUrl
  366. }/project/${opts.project._id.toString()}`
  367. return projectUrl
  368. }
  369. })
  370. templates.userOnboardingEmail = NoCTAEmailTemplate({
  371. subject(opts) {
  372. return `Getting more out of ${settings.appName}`
  373. },
  374. greeting(opts) {
  375. return ''
  376. },
  377. title(opts) {
  378. return `Getting more out of ${settings.appName}`
  379. },
  380. message(opts, isPlainText) {
  381. const learnLatexLink = EmailMessageHelper.displayLink(
  382. 'Learn LaTeX in 30 minutes',
  383. `${
  384. settings.siteUrl
  385. }/learn/latex/Learn_LaTeX_in_30_minutes?utm_source=overleaf&utm_medium=email&utm_campaign=onboarding`,
  386. isPlainText
  387. )
  388. const templatesLinks = EmailMessageHelper.displayLink(
  389. 'Find a beautiful template',
  390. `${
  391. settings.siteUrl
  392. }/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. `${
  398. settings.siteUrl
  399. }/learn/how-to/Sharing_a_project?utm_source=overleaf&utm_medium=email&utm_campaign=onboarding`,
  400. isPlainText
  401. )
  402. const siteLink = EmailMessageHelper.displayLink(
  403. 'www.overleaf.com',
  404. settings.siteUrl,
  405. isPlainText
  406. )
  407. const userSettingsLink = EmailMessageHelper.displayLink(
  408. 'here',
  409. `${settings.siteUrl}/user/settings`,
  410. isPlainText
  411. )
  412. const onboardingSurveyLink = EmailMessageHelper.displayLink(
  413. 'Join our user feedback programme',
  414. 'https://forms.gle/DB7pdk2B1VFQqVVB9',
  415. isPlainText
  416. )
  417. return [
  418. `Thanks for signing up for ${
  419. settings.appName
  420. } recently. We hope you've been finding it useful! Here are some key features to help you get the most out of the service:`,
  421. `${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!`,
  422. `${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.`,
  423. `${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.`,
  424. `${onboardingSurveyLink} to help us make Overleaf even better!`,
  425. 'Thanks again for using Overleaf :)',
  426. `John`,
  427. `Dr John Hammersley <br />Co-founder & CEO <br />${siteLink}<hr>`,
  428. `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}.`
  429. ]
  430. }
  431. })
  432. templates.securityAlert = NoCTAEmailTemplate({
  433. subject(opts) {
  434. return `Overleaf security note: ${opts.action}`
  435. },
  436. title(opts) {
  437. return opts.action.charAt(0).toUpperCase() + opts.action.slice(1)
  438. },
  439. message(opts, isPlainText) {
  440. const dateFormatted = moment().format('dddd D MMMM YYYY')
  441. const timeFormatted = moment().format('HH:mm')
  442. const helpLink = EmailMessageHelper.displayLink(
  443. 'quick guide',
  444. `${settings.siteUrl}/learn/how-to/Keeping_your_account_secure`,
  445. isPlainText
  446. )
  447. const actionDescribed = EmailMessageHelper.cleanHTML(
  448. opts.actionDescribed,
  449. isPlainText
  450. )
  451. if (!opts.message) {
  452. opts.message = []
  453. }
  454. const message = opts.message.map(m => {
  455. return EmailMessageHelper.cleanHTML(m, isPlainText)
  456. })
  457. return [
  458. `We are writing to let you know that ${actionDescribed} on ${dateFormatted} at ${timeFormatted} GMT.`,
  459. ...message,
  460. `If this was you, you can ignore this email.`,
  461. `If this was not you, we recommend getting in touch with our support team at ${
  462. settings.adminEmail
  463. } to report this as potentially suspicious activity on your account.`,
  464. `We also encourage you to read our ${helpLink} to keeping your ${
  465. settings.appName
  466. } account safe.`
  467. ]
  468. }
  469. })
  470. function _formatUserNameAndEmail(user, placeholder) {
  471. if (user.first_name && user.last_name) {
  472. const fullName = `${user.first_name} ${user.last_name}`
  473. if (SpamSafe.isSafeUserName(fullName)) {
  474. if (SpamSafe.isSafeEmail(user.email)) {
  475. return `${fullName} (${user.email})`
  476. } else {
  477. return fullName
  478. }
  479. }
  480. }
  481. return SpamSafe.safeEmail(user.email, placeholder)
  482. }
  483. module.exports = {
  484. templates,
  485. ctaTemplate,
  486. NoCTAEmailTemplate,
  487. buildEmail
  488. }