liuyu пре 5 година
родитељ
комит
a2b59ccfda
3 измењених фајлова са 33 додато и 0 уклоњено
  1. 29 0
      app/service/util.js
  2. 3 0
      config/config.default.js
  3. 1 0
      package.json

+ 29 - 0
app/service/util.js

@@ -7,6 +7,7 @@ const { ObjectId } = require('mongoose').Types;
 const { CrudService } = require('naf-framework-mongoose/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const moment = require('moment');
+const nodemailer = require('nodemailer');
 
 class UtilService extends CrudService {
 
@@ -16,6 +17,34 @@ class UtilService extends CrudService {
     return date;
   }
 
+  async sendMail(email, subject, text, html) {
+    const user_email = this.ctx.app.config.user_email;
+    const auth_code = this.ctx.app.config.auth_code;
+    const transporter = nodemailer.createTransport({
+      service: 'qq',
+      secureConnection: true,
+      port: 465,
+      auth: {
+        user: user_email, // 账号
+        pass: auth_code, // 授权码
+
+      },
+    });
+    const mailOptions = {
+      from: user_email, // 发送者,与上面的user一致
+      to: email, // 接收者,可以同时发送多个,以逗号隔开
+      subject, // 标题
+      text, // 文本
+      html,
+    };
+
+    try {
+      await transporter.sendMail(mailOptions);
+      return true;
+    } catch (err) {
+      return false;
+    }
+  }
 }
 
 module.exports = UtilService;

+ 3 - 0
config/config.default.js

@@ -55,6 +55,9 @@ module.exports = appInfo => {
   config.appid = 'wxdf3ed83c095be97a';
   config.REVIEW_TEMPLATE_ID = 'oqmozBcdkCzBOvNrdzMQ1lDJcobD5uBEpNoLWec0bPY';
 
+  // 邮箱配置
+  config.user_email = '1345526645@qq.com';
+  config.auth_code = 'vnrzrxwserhyfeda';
   // config.amqp = {
   //   client: {
   //     hostname: '127.0.0.1',

+ 1 - 0
package.json

@@ -13,6 +13,7 @@
     "jsonwebtoken": "^8.5.1",
     "lodash": "^4.17.15",
     "naf-framework-mongoose": "^0.6.11",
+    "nodemailer": "^6.4.3",
     "silly-datetime": "^0.1.2",
     "string-random": "^0.1.3",
     "url-join": "^4.0.1",