npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hemera-email

v1.0.1

Published

hemera 邮件处理

Downloads

2

Readme

hemera-email

hemera邮件服务处理

hemera-email 插件实现

插件

'use strict'

const hp = require('hemera-plugin')
const HemeraAjv = require('hemera-ajv')
const nodemailer = require('nodemailer')
const smtpTransport = require('nodemailer-smtp-transport')
module.exports = hp((hemera, options, done) => {
  hemera.use(HemeraAjv)
  hemera.use(require('./schema'))
  const defaultoptions = {
    transport: {
      host: '127.0.0.1',
      port: 25,
      secure: true
    }
  }
  options = !options.transport ? defaultoptions : options
  try {
    const transporter = nodemailer.createTransport(smtpTransport(options.transport))
    hemera.decorate('mail', transporter)
    hemera.use(require('./server'))
  } catch (err) {
    hemera.log.fatal(err)
    hemera.close()
  }
  done()
}, {
  hemera: '>=5.0.0',
  name: require('./package.json').name,
  options: {
    transport: {
      jsonTransport: true
    }
  }
})

服务端实现

const hp = require('hemera-plugin')
module.exports = hp((hemera, opts, done) => {
  const transporter = hemera.mail
  const topic = 'mail'
  hemera.add({
    topic,
    cmd: 'send',
    schema: {
      request: 'messageSchema#'
    }
  }, function (req, cb) {
    //cb(null, req)
    transporter.verify(function (err, success) {
      if (err && !success) {
        cb(err)
      } else {
        transporter.sendMail(req.data, function(err, info) {
          if (err) {
            cb(err)
          }else{
            cb(null, info)
          }
          done()
        })
      }
    })
  })
  done()
}, {
  hemera: '>=5.0.0',
  name: 'server',
  options: {
  }
})

注册插件

const tap = require("tap")
const Hemera = require('nats-hemera')
const Nats = require("hemera-testsuite/nats")
const nats = new Nats()
const hemera = new Hemera(nats)
hemera.register(require("../index"),
{
  transport: {
    name: '测试邮件',
    // logger: true, //是否开启日志
    // debug: true, //开启debug调试信息
    connectionTimeout: 36000, //等待连接建立的毫秒数
    greetingTimeout: 16000, //建立连接后等待问候语的毫秒数
    socketTimeout: 3600000, //允许多少毫秒的不活动状态
    pool: true, //开启连接池|sendmail | streamTransport | jsonTransport | SES
    maxConnections: 100, //是针对SMTP服务器建立的最大同时连接数(默认为5)
    maxMessages: 200,//  限制使用单个连接发送的消息数(默认为100)。达到maxMessages后,将断开连接并为以下消息创建一个新的连接
    rateDelta: 2000,//定义用于速率限制的时间测量周期(以毫秒为单位)(默认为1000,即1秒)
    host: 'smtp.qq.com',
    port: 465,
    secure: true, //只有端口为587的时候为false,其余端口为true
    auth: {
      type: 'custom',
      method: 'MY-CUSTOM-METHOD', //强制Nodemailer使用您的自定义处理程序
      user: '[email protected]',
      pass: 'password'
    },
    customAuth: {
      'MY-CUSTOM-METHOD': myCustomMethod //自定义处理函数
    },
    // tls: {
    //   // 使用自签名或无效的TLS证书打开与TLS服务器的连接
    //   rejectUnauthorized: false
    // }
    //disableFileAccess: true, //如果为true,则不允许使用文件作为内容。当您要将不可信来源的JSON数据用作电子邮件时,请使用它。如果附件或消息节点尝试从文件中获取内容,则发送将返回错误。如果在传输选项中也设置了该字段,则邮件数据中的值将被忽略
    //disableUrlAccess: true //如果为true,则不允许将Urls用作内容。如果在传输选项中也设置了该字段,则邮件数据中的值将被忽略
  }
})

具体使用

hemera.ready(() => {
  let messageId = UUID.v1()
  hemera.act({
    topic: 'mail',
    cmd: 'send',
    data: {
      from: '[email protected]', //发件人邮箱地址
      to: ['[email protected]', '[email protected]'], //收件人邮箱地址,可以是列表
      cc: ['[email protected]'], //抄送人邮箱地址,可以是列表
      bcc: '[email protected]', //密件抄送人邮箱地址,可以是列表
      messageId: messageId,
      subject: '发送简单文本', //邮件主题
      text: '发送简单文本', //消息的明文版本作为Unicode字符串,缓冲液,流或附件状物体 {路径:“的/ var /数据/ ...”} 
      html: '<p>HTML version of the message</p>', //消息的HTML版本,如Unicode字符串,Buffer,Stream或类似附件的对象 {path:'http://…'}
    }}, function (err, resp) {
  })

  messageId = UUID.v1()
  hemera.act({
    topic: 'mail',
    cmd: 'send',
    data: {
      from: '[email protected]', //发件人邮箱地址
      to: ['[email protected]', '[email protected]'], //收件人邮箱地址,可以是列表
      cc: ['[email protected]'], //抄送人邮箱地址,可以是列表
      bcc: '[email protected]', //密件抄送人邮箱地址,可以是列表
      subject: '发送带附件邮件', //邮件主题
      text: '发送带附件邮件', //消息的明文版本作为Unicode字符串,缓冲液,流或附件状物体 {路径:“的/ var /数据/ ...”} 
      html: '<p>HTML version of the message</p>', //消息的HTML版本,如Unicode字符串,Buffer,Stream或类似附件的对象 {path:'http://…'}
      messageId: messageId,
      attachments: [
        {
          filename: '纯字符串文本.txt', //自定义附件名以纯文本形式
          content: 'hello world!'
        },
        {
          filename: '自定义文件名.txt', //自定义附件名以读取文件形式
          path: 'test.txt'
        }
      ]
    }
  }, function (err, resp) {})
})