email.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /// 邮件发送接口封装
  2. const { format } = require('date-fns');
  3. const utils = require('../../libs/utils');
  4. const ejs = require('ejs');
  5. async function sendEmail(title, data) {
  6. await reportEmail(title, data);
  7. }
  8. async function reportEmail(title, data) {
  9. let html = await renderFile(__dirname + '/monitor.ejs', {
  10. title,
  11. data,
  12. now: new Date(),
  13. format: (str, fmt) => {
  14. fmt = 'yyyy/M/d HH:mm'
  15. return format(new Date(str), fmt);
  16. }
  17. });
  18. let res = await utils.mail.send({
  19. to: 'chengen@jccy-tech.com,yangshuai@jccy-tech.com,chenxinmiao@jccy-tech.com,wangmeng@jccy-tech.com;guoziyun@jccy-tech.com',
  20. // to: 'guoziyun@jccy-tech.com',
  21. subject: title,
  22. html,
  23. })
  24. return res;
  25. }
  26. async function renderFile(filename, data, options) {
  27. return new Promise((done, reject) => {
  28. ejs.renderFile(filename, data, options, function (err, str) {
  29. if (err) reject(err);
  30. else done(str);
  31. });
  32. })
  33. }
  34. module.exports = { sendEmail }
  35. if (require.main == module) {
  36. sendEmail("Monitor Heartbeat", "heartbeat");
  37. }