egg-nodemailer-extra

1.2.4 • Public • Published

egg-nodemailer

NPM version build status Test coverage David deps Known Vulnerabilities npm download

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: 'example@qq.com', // 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: 'example@qq.com', // sender address
      to: 'youremail@qq.com', // 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.

License

MIT

Package Sidebar

Install

npm i egg-nodemailer-extra

Weekly Downloads

2

Version

1.2.4

License

MIT

Last publish

Collaborators

  • zongmingli