mail.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. const nodemailer = require("nodemailer");
  2. const path = require("path");
  3. const fs = require("fs");
  4. const { resetMailText, verifyMailText, changeEmailText } = require("./text");
  5. const { CustomError } = require("../utils");
  6. const env = require("../env");
  7. const mailConfig = {
  8. host: env.MAIL_HOST,
  9. port: env.MAIL_PORT,
  10. secure: env.MAIL_SECURE,
  11. auth: env.MAIL_USER
  12. ? {
  13. user: env.MAIL_USER,
  14. pass: env.MAIL_PASSWORD
  15. }
  16. : undefined
  17. };
  18. const transporter = nodemailer.createTransport(mailConfig);
  19. // Read email templates
  20. const resetEmailTemplatePath = path.join(__dirname, "template-reset.html");
  21. const verifyEmailTemplatePath = path.join(__dirname, "template-verify.html");
  22. const changeEmailTemplatePath = path.join(__dirname,"template-change-email.html");
  23. const resetEmailTemplate = fs
  24. .readFileSync(resetEmailTemplatePath, { encoding: "utf-8" })
  25. .replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
  26. .replace(/{{site_name}}/gm, env.SITE_NAME);
  27. const verifyEmailTemplate = fs
  28. .readFileSync(verifyEmailTemplatePath, { encoding: "utf-8" })
  29. .replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
  30. .replace(/{{site_name}}/gm, env.SITE_NAME);
  31. const changeEmailTemplate = fs
  32. .readFileSync(changeEmailTemplatePath, { encoding: "utf-8" })
  33. .replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
  34. .replace(/{{site_name}}/gm, env.SITE_NAME);
  35. async function verification(user) {
  36. const mail = await transporter.sendMail({
  37. from: env.MAIL_FROM || env.MAIL_USER,
  38. to: user.email,
  39. subject: "Verify your account",
  40. text: verifyMailText
  41. .replace(/{{verification}}/gim, user.verification_token)
  42. .replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
  43. .replace(/{{site_name}}/gm, env.SITE_NAME),
  44. html: verifyEmailTemplate
  45. .replace(/{{verification}}/gim, user.verification_token)
  46. .replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
  47. .replace(/{{site_name}}/gm, env.SITE_NAME)
  48. });
  49. if (!mail.accepted.length) {
  50. throw new CustomError("Couldn't send verification email. Try again later.");
  51. }
  52. }
  53. async function changeEmail(user) {
  54. const mail = await transporter.sendMail({
  55. from: env.MAIL_FROM || env.MAIL_USER,
  56. to: user.change_email_address,
  57. subject: "Verify your new email address",
  58. text: changeEmailText
  59. .replace(/{{verification}}/gim, user.change_email_token)
  60. .replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
  61. .replace(/{{site_name}}/gm, env.SITE_NAME),
  62. html: changeEmailTemplate
  63. .replace(/{{verification}}/gim, user.change_email_token)
  64. .replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
  65. .replace(/{{site_name}}/gm, env.SITE_NAME)
  66. });
  67. if (!mail.accepted.length) {
  68. throw new CustomError("Couldn't send verification email. Try again later.");
  69. }
  70. }
  71. async function resetPasswordToken(user) {
  72. const mail = await transporter.sendMail({
  73. from: env.MAIL_FROM || env.MAIL_USER,
  74. to: user.email,
  75. subject: "Reset your password",
  76. text: resetMailText
  77. .replace(/{{resetpassword}}/gm, user.reset_password_token)
  78. .replace(/{{domain}}/gm, env.DEFAULT_DOMAIN),
  79. html: resetEmailTemplate
  80. .replace(/{{resetpassword}}/gm, user.reset_password_token)
  81. .replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
  82. });
  83. if (!mail.accepted.length) {
  84. throw new CustomError(
  85. "Couldn't send reset password email. Try again later."
  86. );
  87. }
  88. }
  89. async function sendReportEmail(link) {
  90. const mail = await transporter.sendMail({
  91. from: env.MAIL_FROM || env.MAIL_USER,
  92. to: env.REPORT_EMAIL,
  93. subject: "[REPORT]",
  94. text: link,
  95. html: link
  96. });
  97. if (!mail.accepted.length) {
  98. throw new CustomError("Couldn't submit the report. Try again later.");
  99. }
  100. }
  101. module.exports = {
  102. changeEmail,
  103. verification,
  104. resetPasswordToken,
  105. sendReportEmail,
  106. }