1.0.2 • Published 6 years ago

think-mailer v1.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

think-mailer

npm Build Status Coverage Status NPM version Downloads

Thinkjs mailer adapter

Development based on nodemailer

Install

yarn add think-mailer --save
#npm install think-mailer --save
#cnpm install think-mailer --save

Config

ThinkJSProjectRoot/src/config/adapter.js

exports.mailer = {
  type: 'mailer',
  mailer: {
    host: 'smtp.xxx.com',
    port: 465,
    secure: true,
    auth: {
      user: 'xxx@xxx.com', // your account
      pass: 'JfoBrEMBYkzhvzRB' // authorization code, not the email password
    },
    tls: {
      rejectUnauthorized: false
    }
  }
};

ThinkJSProjectRoot/src/config/extend.js

const view = require('think-view');
const cache = require('think-cache');
const session = require('think-session');
const mongo = require('think-mongo');
const email = require('think-mailer');

module.exports = [view, mongo(think.app), cache, session, email(think.app)];

Send Mail

ThinkJSProjectRoot/src/controller/xxx.js

const Base = require('./base.js');

module.exports = class extends Base {
  indexAction() {
    const ctx = this
    const mailer = ctx.mailer()
    return mailer.send({
      from: 'xxx@xxx.com',
      to: 'xxx@xxx.com',
      subject: 'Email Tile',
      html: '<p>Email content</p>'
    })
    .then(async function(res) {
      return ctx.success({
        msg: 'Email Sent.'
      })
    })
    .catch(function(err) {
      console.log(err)
      return ctx.fail(1000, 'Email send failed.')
    })
  }
};