如何使 nodemailer 在多个模块中可重用?
How to make nodemailer reusable in multiple module?
我在用户注册后实现了nodemailer
,方式如下:
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD
}
});
let mailOptions = {
from: process.env.EMAIL_USERNAME,
to: user.email,
subject: 'Verify your account',
text: 'Click here for verify your account'
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
});
我不太喜欢这段代码,因为如果我需要在另一个 module
中发送电子邮件,我需要重写上面的所有内容。
由于我是 NodeJS
的新手,我想知道我是否可以删除此代码冗余,使之成为 utility
或 helper
class.目标是导入 wrapper
class 并调用一个简单的函数来发送电子邮件。
最好的处理方法是什么?
您可以使用 module.exports 如下:
创建公共服务 mail.js 并在此处编写您的邮件发送代码
mails.js
module.exports = function (){
// mail sent code
}
要求mail.js在其他服务中编写邮件发送代码并调用邮件发送功能
otherService.js
var mail = require('mail.js') // require mail sent in other service where you want to send mail
mail.sent() // call function of mail.js
我将您的代码重构为如下所示,然后将其另存为 mail.js
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD
}
});
let sendMail = (mailOptions)=>{
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
});
};
module.exports = sendMail;
在您的其他模块中,说 activation.js
var mailer = require('./mail.js');
mailer({
from: process.env.EMAIL_USERNAME,
to: user.email,
subject: 'Verify your account',
text: 'Click here for verify your account'
};);
我为此创建了一个class:
import NodeMailer from 'nodemailer'
import emailConfig from '../../config/mail' // read email credentials from your config
class EmailSender {
transport
constructor() {
this.transport = NodeMailer.createTransport({
host: emailConfig.MAIL_HOST,
port: emailConfig.MAIL_PORT,
auth: {
user: emailConfig.MAIL_USERNAME,
pass: emailConfig.MAIL_PASSWORD,
},
})
}
async sendMessage(to, subject, text, html) {
let mailOptions = {
from: emailConfig.MAIL_FROM_ADDRESS,
to,
subject,
text,
html,
}
await this.transport.sendMail(mailOptions)
}
}
export default new EmailSender()
现在你可以在你的任意路由中实现它了:
router.get('/email', async (req, res) => {
try {
await EmailSender.sendMessage(
'bijaya@bijaya.com',
'Hello world',
'test',
'<h1>Test</h1>'
)
return res.status(200).send('Successfully sent email.')
} catch (exception) {
return res.status(500).send(exception.message)
}
})
我在用户注册后实现了nodemailer
,方式如下:
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD
}
});
let mailOptions = {
from: process.env.EMAIL_USERNAME,
to: user.email,
subject: 'Verify your account',
text: 'Click here for verify your account'
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
});
我不太喜欢这段代码,因为如果我需要在另一个 module
中发送电子邮件,我需要重写上面的所有内容。
由于我是 NodeJS
的新手,我想知道我是否可以删除此代码冗余,使之成为 utility
或 helper
class.目标是导入 wrapper
class 并调用一个简单的函数来发送电子邮件。
最好的处理方法是什么?
您可以使用 module.exports 如下:
创建公共服务 mail.js 并在此处编写您的邮件发送代码
mails.js
module.exports = function (){
// mail sent code
}
要求mail.js在其他服务中编写邮件发送代码并调用邮件发送功能
otherService.js
var mail = require('mail.js') // require mail sent in other service where you want to send mail
mail.sent() // call function of mail.js
我将您的代码重构为如下所示,然后将其另存为 mail.js
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD
}
});
let sendMail = (mailOptions)=>{
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
});
};
module.exports = sendMail;
在您的其他模块中,说 activation.js
var mailer = require('./mail.js');
mailer({
from: process.env.EMAIL_USERNAME,
to: user.email,
subject: 'Verify your account',
text: 'Click here for verify your account'
};);
我为此创建了一个class:
import NodeMailer from 'nodemailer'
import emailConfig from '../../config/mail' // read email credentials from your config
class EmailSender {
transport
constructor() {
this.transport = NodeMailer.createTransport({
host: emailConfig.MAIL_HOST,
port: emailConfig.MAIL_PORT,
auth: {
user: emailConfig.MAIL_USERNAME,
pass: emailConfig.MAIL_PASSWORD,
},
})
}
async sendMessage(to, subject, text, html) {
let mailOptions = {
from: emailConfig.MAIL_FROM_ADDRESS,
to,
subject,
text,
html,
}
await this.transport.sendMail(mailOptions)
}
}
export default new EmailSender()
现在你可以在你的任意路由中实现它了:
router.get('/email', async (req, res) => {
try {
await EmailSender.sendMessage(
'bijaya@bijaya.com',
'Hello world',
'test',
'<h1>Test</h1>'
)
return res.status(200).send('Successfully sent email.')
} catch (exception) {
return res.status(500).send(exception.message)
}
})