nodemailer plugin for Egg.js.
$ npm i egg-mailer --save
// {app_root}/config/plugin.js
exports.mailer = {
enable: true,
package: 'egg-mailer',
};
// {app_root}/config/config.default.js
exports.mailer = {
host: "smtp.ethereal.email",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: testAccount.user, // generated ethereal user
pass: testAccount.pass // generated ethereal password
}
};
see nodemailer for more detail.
// app/controller/home.js
class HomeController extends Controller {
async index() {
const { ctx, app } = this;
// sync
await app.mailer.send({
from: '"Fred Foo π»" <foo@example.com>', // sender address, [options] default to user
// // Array => ['bar@example.com', 'baz@example.com']
to: "bar@example.com, baz@example.com", // list of receivers
subject: "Hello β", // Subject line
text: "Hello world?", // plain text body
html: "<b>Hello world?</b>" // html body
});
// async
app.mailer.send({
from: '"Fred Foo π»" <foo@example.com>',
// Array => ['bar@example.com', 'baz@example.com']
to: "bar@example.com, baz@example.com",
subject: "Hello β",
text: "Hello world?",
html: "<b>Hello world?</b>"
}, function (err, info) {
if (err) {
throw err;
}
console.log(info);
});
ctx.body = 'hi, mailer';
}
}
Please open an issue here.