From 0da42438120c46f99148d4a6bcced0e09ae59d52 Mon Sep 17 00:00:00 2001 From: hzz780 Date: Thu, 29 Sep 2022 14:26:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20upgrade=20nodemailer=20&?= =?UTF-8?q?=20add=20smtp=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.dev.js | 14 ++++++++++++-- config.prod.js | 14 ++++++++++++-- package.json | 2 +- src/emails.js | 19 +++++++++++++------ 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/config.dev.js b/config.dev.js index 879ad8a..241a59e 100644 --- a/config.dev.js +++ b/config.dev.js @@ -66,10 +66,20 @@ module.exports = { blockApi: 'http://127.0.0.1:7101', // 报错重启时的邮件发送 mails: { + type: 'smtp', // smtp | sendmail sendmailPath: '/usr/sbin/sendmail', - user: 'aelf.scan@aelf.io', // generated ethereal user + user: 'scan@domain.io', // generated ethereal user from: 'AElf scan ', to: ['test@mail.com'], - subject: 'error happened when scanning' + subject: 'error happened when scanning', // mail subject + smtpConfig: { + host: "smtp.domain.com", + port: 465, + secure: true, + auth: { + user: "monitor@domain.com", + pass: "password", + } + } } }; diff --git a/config.prod.js b/config.prod.js index 266008d..95504ed 100644 --- a/config.prod.js +++ b/config.prod.js @@ -57,10 +57,20 @@ module.exports = { }, blockApi: 'http://127.0.0.1:7101', mails: { + type: 'smtp', // smtp | sendmail sendmailPath: '/usr/sbin/sendmail', - user: 'aelf.scan@aelf.io', // generated ethereal user + user: 'scan@domain.io', // generated ethereal user from: 'AElf scan ', to: ['test@mail.com'], - subject: 'error happened when scanning' + subject: 'error happened when scanning', + smtpConfig: { + host: "smtp.domain.com", + port: 465, + secure: true, + auth: { + user: "monitor@domain.com", + pass: "password", + } + } } }; diff --git a/package.json b/package.json index df6dce8..8422e2d 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "long": "^4.0.0", "moment": "^2.24.0", "mysql": "^2.17.1", - "nodemailer": "^6.3.0", + "nodemailer": "^6.8.0", "pm2": "^4.2.1", "redis": "^2.8.0" }, diff --git a/src/emails.js b/src/emails.js index 74e416f..5638971 100644 --- a/src/emails.js +++ b/src/emails.js @@ -11,17 +11,24 @@ const mailConfig = config.mails; async function sendEmails(message = '') { const { + type, from, to, subject, - sendmailPath + sendmailPath, + smtpConfig } = mailConfig; try { - const transporter = nodemailer.createTransport({ - sendmail: true, - newline: 'unix', - path: sendmailPath - }); + let transporter; + if (type === 'smtp') { + transporter = nodemailer.createTransport(smtpConfig); + } else { + transporter = nodemailer.createTransport({ + sendmail: true, + newline: 'unix', + path: sendmailPath + }); + } // send mail with defined transport object const info = await transporter.sendMail({