util.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. const moment = require('moment');
  8. const nodemailer = require('nodemailer');
  9. class UtilService extends CrudService {
  10. async updatedate() {
  11. let date = new Date();
  12. date = moment(date).format('YYYY-MM-DD HH:mm:ss');
  13. return date;
  14. }
  15. async sendMail(email, subject, text, html) {
  16. const user_email = this.ctx.app.config.user_email;
  17. const auth_code = this.ctx.app.config.auth_code;
  18. const transporter = nodemailer.createTransport({
  19. host: 'smtp.exmail.qq.com',
  20. secureConnection: true,
  21. port: 465,
  22. auth: {
  23. user: user_email, // 账号
  24. pass: auth_code, // 授权码
  25. },
  26. });
  27. const mailOptions = {
  28. from: user_email, // 发送者,与上面的user一致
  29. to: email, // 接收者,可以同时发送多个,以逗号隔开
  30. subject, // 标题
  31. text, // 文本
  32. html,
  33. };
  34. try {
  35. await transporter.sendMail(mailOptions);
  36. return true;
  37. } catch (err) {
  38. return false;
  39. }
  40. }
  41. }
  42. module.exports = UtilService;