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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@godotdotdot/egg-nodemailer

v1.0.0

Published

nodemailer plugin for Egg.js.

Downloads

6

Readme

egg-nodemailer

egg-nodemailer 是一个基于 nodemailer 的 egg plugin。

如何使用

配置

要使用插件请在 config/plugin 文件下配置该插件:

// {app_root}/config/plugin.js
nodemailer: {
  enable: true,
  package: '@godotdotdot/egg-nodemailer',
},
// 注意,egg-nodemailer依赖 egg-view-ejs 插件,请务必安装
// 否则将会启动失败
ejs: {
  enable: true,
  package: 'egg-view-ejs',
},

配置,同 nodemailer.createTransport 函数入参:

// {app_root}/config/config.xxx.js
nodemailer: {
    client: {
      host: 'smtp.exmail.qq.com',
      port: 465,
      secure: true, // true for 465, false for other ports
      auth: {
          user: '[email protected]',
          pass: 'password'
      },
      tls: {
        // do not fail on invalid certs
        rejectUnauthorized: false
      }
    },
},
// 配置 egg-view 插件启用 ejs 模板引擎
view: {
    mapping: {
      '.ejs': 'ejs',
    },
}

如果需要配置多 client,请参考下面示例:

// {app_root}/config/config.xxx.js
nodemailer: {
    clients: {
      a: {
          host: 'smtp.exmail.qq.com',
          port: 465,
          secure: true, // true for 465, false for other ports
          auth: {
              user: '[email protected]', // generated ethereal user
              pass: 'password' // generated ethereal password
          },
          tls: {
            // do not fail on invalid certs
            rejectUnauthorized: false
          }
        },
      b: {
        host: 'smtp.exmail.qq.com',
        port: 465,
        secure: true, // true for 465, false for other ports
        auth: {
            user: '[email protected]', // generated ethereal user
            pass: 'password' // generated ethereal password
        },
        tls: {
          // do not fail on invalid certs
          rejectUnauthorized: false
        }
      },
    },
},

使用 nodemailer

egg-nodemailer 插件将 NodeEmail 对象挂载到 Application 对象上,挂载的属性名为 nodemailer。即可通过 this.app.nodemailer 获取。如果是多 clients ,请通过 this.app.nodemailer.get(key) 来获取 NodeEmail 对象。

NodeEmail 暴露了两个常用接口用于邮件操作:

sendMail(options)

发送邮件

options 为一个对象,对象具有的属性为:

| 参数 | 类型 | 备注 | | ----------- | ------------------------------------------------ | --------------------------------------------------------------------- | | mailOptions | NodeMailerOptions | 邮件配置项,具体配置可查看 ts 定义文件 | | transport | Mail | 可选,SMTP transport 对象,未赋值时,将会根据 nodemailer 配置自动创建 | | config | SMTPTransport | SMTPTransport.Options | string | 可选,SMTP 连接配置项,未赋值时,将会取 nodemailer 配置 | | defaults | SMTPTransport.Options | 可选,SMTP transport options |

返回:

| 字段 | 类型 | 备注 | | ------- | ------ | -------------- | | content | string | 发送的邮件内容 | | result | any | 邮件发送结果 |

温馨提示:

egg-nodemailer 内置邮件模板支持,模板文件统一放置在 html-template 文件夹下。模板默认使用 ejs 模板引擎。如需使用模板,需要在 mailOptions 中配置 htmlTemplateNamehtmlTemplateData 两个字段。

htmlTemplateName: 即模板名称,如 weekly.ejs

htmlTemplateData:即 ejs 模板数据。

creatTransport

创建 SMTP connection 对象。

形参:

| 参数 | 类型 | 备注 | | -------- | ------------------------------------------------ | ----------------------------------------------------------------------- | | config | SMTPTransport | SMTPTransport.Options | string | 可选,SMTP transport 邮箱服务器配置项,未赋值时,将会取 nodemailer 配置 | | defaults | SMTPTransport.Options | 可选,SMTP transport options |

返回:

Mail 对象

verify

验证 SMTP 邮件服务器配置是否有效。如果正常,则返回 true,否则返回 Error 错误对象。参考https://nodemailer.com/smtp/#verify-smtp-connection-configuration

形参:

| 参数 | 类型 | 备注 | | --------- | ---- | --------------------------------------------------------------------- | | transport | Mail | 可选,SMTP transport 对象,未赋值时,将会根据 nodemailer 配置自动创建 |

返回:

Promise

示例

Controller:

	// 多实例
	async multMail() {
    this.ctx.body = (await this.app.nodemailer.get('a').sendMail({
      mailOptions:{
        from: '[email protected]',
        to: '[email protected]',
        subject: '[周报][godotdotdot][2019/08/19 ~ 2019/08/23]',
        htmlTemplateName: 'weekly.ejs',
        htmlTemplateData: {
          user: 'godotdotdot',
          startDate: new Date().toLocaleDateString(),
          endDate: new Date().toLocaleDateString(),
          projects: [
            {
              name: 'Mete Work',
              content: `<p>Mete Work</p>`,
            },
          ],
          technology: [],
          jjyy: `JJWW`,
      },}
    })).content;
  }
	// 单实例
  async singleMail() {
    this.ctx.body = (await this.app.nodemailer.sendMail({
      mailOptions: {
        from: '[email protected]',
        to: '[email protected]',
        subject: '[周报][godotdotdot][2019/08/19 ~ 2019/08/23]',
        htmlTemplateName: 'weekly.ejs',
        htmlTemplateData: {
          user: 'godotdotdot',
          startDate: new Date().toLocaleDateString(),
          endDate: new Date().toLocaleDateString(),
          projects: [
            {
              name: 'Lucifer 原型图',
              content: `<p>Lucifer 研发流程平台原型图产出</p>`,
            },
          ],
          technology: [],
          jjyy: `这里是叽叽歪歪`,
        },
      }
    })).content;
  }

	// 使用其他 transport 邮箱服务器配置
  async create() {
    const transport = this.app.nodemailer.createTransport({
      host: 'smtp.exmail.qq.com',
      port: 465,
      secure: true,
      auth: {
        user: '[email protected]',
        pass: 'password',
      },
      tls: {
        rejectUnauthorized: false,
      },
    });

    this.ctx.body = (await this.app.nodemailer.sendMail({
      mailOptions: {
        from: '[email protected]',
        to: '[email protected]',
        subject: '[周报][godotdotdot][2019/08/19 ~ 2019/08/23]', // Subject line
        htmlTemplateName: 'weekly.ejs',
        htmlTemplateData: {
          user: 'godotdotdot',
          startDate: new Date().toLocaleDateString(),
          endDate: new Date().toLocaleDateString(),
          projects: [
            {
              name: 'Mete Work',
              content: `<p>Mete Work</p>`,
            },
          ],
          technology: [],
          jjyy: `JJWW`,
      },
      transprot,
    }})).content;
  }