email.service.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // api/emails/email.service.js
  2. import * as fs from "fs";
  3. import { Liquid } from "liquidjs";
  4. import i18n from "../../common/i18n.js";
  5. import transporter from "../../common/transporter.js";
  6. import BaseService from "../../services/base.service.js";
  7. import Email from "./email.model.js";
  8. class EmailService extends BaseService {
  9. getModel() {
  10. return Email;
  11. }
  12. async loadEmail(code, lang = "en") {
  13. let email = await this.oneBy({ code: code, lang: lang });
  14. if (!email) {
  15. email = {
  16. body: fs.readFileSync(`views/mailer/${code}.email.liquid`, "utf8"),
  17. };
  18. }
  19. return email;
  20. }
  21. async forgotPasswordLink(user) {
  22. const data = await this.loadEmail("forgotPassword", user.language);
  23. const engine = new Liquid();
  24. const emailText = await engine.parseAndRender(data.body, {
  25. email: user.email,
  26. passwordResetToken: user.passwordResetToken,
  27. });
  28. const mailOptions = {
  29. to: user.email,
  30. from: process.env.DEFAULT_EMAIL_FROM,
  31. subject: data.subject || "[StarterSaaS] Reset password code",
  32. html: emailText,
  33. };
  34. try {
  35. const result = await transporter.sendMail(mailOptions);
  36. console.log('Email sent to:', user.email, 'Subject:', mailOptions.subject);
  37. console.log('Email sent: ' + result.response);
  38. return result;
  39. } catch (error) {
  40. console.log('Error sending email to:', user.email, 'Error:', error);
  41. throw error;
  42. }
  43. }
  44. async sendActivationEmail(user) {
  45. const data = await this.loadEmail("activationLink", user.language);
  46. const engine = new Liquid();
  47. const emailText = await engine.parseAndRender(data.body, {
  48. email: user.email,
  49. confirmationToken: user.confirmationToken,
  50. });
  51. const mailOptions = {
  52. to: user.email,
  53. from: process.env.DEFAULT_EMAIL_FROM,
  54. subject: "[StarterSaaS] Activation code",
  55. html: emailText,
  56. };
  57. try {
  58. const result = await transporter.sendMail(mailOptions);
  59. console.log('Email sent to:', user.email, 'Subject:', mailOptions.subject);
  60. console.log('Email sent: ' + result.response);
  61. return result;
  62. } catch (error) {
  63. console.log('Error sending email to:', user.email, 'Error:', error);
  64. throw error;
  65. }
  66. }
  67. async activated(user) {
  68. const data = await this.loadEmail("activate", user.language);
  69. const engine = new Liquid();
  70. const emailText = await engine.parseAndRender(data.body, {
  71. email: user.email,
  72. frontendLoginURL: process.env.FRONTEND_LOGIN_URL,
  73. });
  74. const mailOptions = {
  75. to: user.email,
  76. from: process.env.DEFAULT_EMAIL_FROM,
  77. subject: data.subject || "[Startersaaskit] Account activated",
  78. html: emailText,
  79. };
  80. try {
  81. const result = await transporter.sendMail(mailOptions);
  82. console.log('Email sent to:', user.email, 'Subject:', mailOptions.subject);
  83. console.log('Email sent: ' + result.response);
  84. return result;
  85. } catch (error) {
  86. console.log('Error sending email to:', user.email, 'Error:', error);
  87. throw error;
  88. }
  89. }
  90. async generalNotification(toEmail, subject, message, locale = i18n.locale()) {
  91. const data = await this.loadEmail("notification", locale);
  92. const engine = new Liquid();
  93. const emailText = await engine.parseAndRender(data.body, {
  94. email: toEmail,
  95. message: message,
  96. frontendLoginURL: process.env.FRONTEND_LOGIN_URL,
  97. });
  98. const mailOptions = {
  99. to: toEmail,
  100. from: process.env.DEFAULT_EMAIL_FROM,
  101. subject: subject,
  102. html: emailText,
  103. };
  104. try {
  105. const result = await transporter.sendMail(mailOptions);
  106. console.log('Email sent to:', toEmail, 'Subject:', mailOptions.subject);
  107. console.log('Email sent: ' + result.response);
  108. return result;
  109. } catch (error) {
  110. console.log('Error sending email to:', toEmail, 'Error:', error);
  111. throw error;
  112. }
  113. }
  114. }
  115. export default new EmailService();