egg-nodemailer-extra
v1.2.4
Published
NodeMailer plugin for egg
Downloads
29
Maintainers
Readme
egg-nodemailer
Install
$ npm i egg-nodemailer --save
Usage
// {app_root}/config/plugin.js
exports.nodemailer = {
enable: true,
package: 'egg-nodemailer-extra',
};
Configuration
// {app_root}/config/config.default.js
exports.nodemailer = {
};
see config/config.default.js for more detail.
Example
// app/config/config.default.js
module.exports = appInfo => {
const config = exports = {};
config.nodemailer = {
host: 'smtp.exmail.qq.com', // your email smtp server
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: '[email protected]', // generated ethereal user
pass: 'password', // generated ethereal password
},
};
return config;
};
// app.js
module.exports = app => {
app.transporter = app.nodemailer.create(app.config.nodemailer, app);
};
// controller/post.js
const Controller = require('egg').Controller;
class PostController extends Controller {
async create() {
const { app } = this;
let mailOptions = {
from: '[email protected]', // sender address
to: '[email protected]', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world?', // plain text body
html: '<b>Hello world?</b>' // html body
};
app.transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info);
// Preview only available when sending through an Ethereal account
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
ctx.body = info;
ctx.status = 200;
});
}
}
module.exports = PostController;
Questions & Suggestions
Please open an issue here.