Skip to content

Commit

Permalink
feat: 🎸 upgrade nodemailer & add smtp logic
Browse files Browse the repository at this point in the history
  • Loading branch information
hzz780 committed Sep 29, 2022
1 parent 5122b8e commit 0da4243
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
14 changes: 12 additions & 2 deletions config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>',
to: ['[email protected]'],
subject: 'error happened when scanning'
subject: 'error happened when scanning', // mail subject
smtpConfig: {
host: "smtp.domain.com",
port: 465,
secure: true,
auth: {
user: "[email protected]",
pass: "password",
}
}
}
};
14 changes: 12 additions & 2 deletions config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>',
to: ['[email protected]'],
subject: 'error happened when scanning'
subject: 'error happened when scanning',
smtpConfig: {
host: "smtp.domain.com",
port: 465,
secure: true,
auth: {
user: "[email protected]",
pass: "password",
}
}
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
19 changes: 13 additions & 6 deletions src/emails.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 0da4243

Please sign in to comment.