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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@feng-j/midwayjs-mailer

v2.0.1

Published

用于发送邮件的MidwayJS的组件

Downloads

10

Readme

MidwayJS-Mailer

用于在MidwayJS项目中快捷使用邮件发送功能。

安装使用

$ npm install nodemailer @types/nodemailer @feng-j/midwayjs-mailer

在configuration中引入mailer

import * as mailer from '@feng-j/midwayjs-mailer';
// ......

@Configuration({
  imports: [
    // ......
    malier,
  ],
  importConfigs: [join(__dirname, './config')],
})
export class MainConfiguration {
  @App('koa')
  app: koa.Application;

  async onReady() {
    this.app.useMiddleware([ReportMiddleware]);
  }
}

在配置文件中添加mailer相关的配置, 配置项直接传给nodemailer, 所以配置与nodemailer createTransport参数一致。

export default {
  // ......
  mailer: {
    host: 'smtp.qq.com',
    secure: true,
    auth: {
        user: '[email protected]',
        pass: 'xxx'
    }
  },
} as MidwayConfig;

然后就可以通过Inject来注入MailerService了

import { Inject, Controller, Get, Query } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { MailerService } from '@feng-j/midwayjs-mailer';

@Controller('/api')
export class APIController {
  @Inject()
  ctx: Context;

  @Inject()
  mailerService: MailerService;

  @Get('/send')
  async sendEmail() {
    await this.mailerService.send({
      subject: 'custom prefix render',
      to: '[email protected]',
      text: 'Hello!',
    })
  }
}

更多使用方法可以见nodemailer 文档

模板渲染

除了基本文本内容邮件的发送,还可以在邮件中嵌入HTML内容,通过模板引擎渲染,目前支持ejspugnunjucks模板引擎的渲染。(如果要是用其他的模板引擎,可以将渲染完成的htmlStr手动传入, 后期可以支持自定义渲染)

以ejs模板引擎为例,在配置文件中开启模板渲染的支持

export default {
  // ......
  mailer: {
    host: 'smtp.qq.com',
    secure: true,
    auth: {
        user: '[email protected]',
        pass: 'xxx'
    },
    template: 'ejs', // 指定模板引擎
  },
} as MidwayConfig;

需要额外安装ejs的依赖库

$ pnpm i ejs

然后准备一个模板文件,例如:

<!doctype html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Order</title>
</head>
<body>
<h3>
  感谢您订购我们的服务,当前订单(<%= orderId %>)已经生效。
</h3>
祝您使用愉快
</body>
</html>

然后在发送邮件的时候传入模板文件路径,以及模板数据,例如:

import { Inject, Controller, Get, Query } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { MailerService } from '@feng-j/midwayjs-mailer';
import { join } from 'path';

@Controller('/api')
export class APIController {
  @Inject()
  ctx: Context;

  @Inject()
  mailerService: MailerService;

  @Get('/send')
  async sendEmail() {
    await mailerService.send(
      {
        subject: 'templateMail',
        from: '小枫 <[email protected]>',
        to: '[email protected]',
      },
      {
        path: join(__dirname, './fixtures/templateMail/src/template/order.ejs'),
        record: { orderId: 'BL20241208160753' },
      }
    );
  }
}

发送邮件效果如下:

img.png