Skip to content

MrDotYan/midwayjs_mailer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

安装依赖

在 src 目录下新建 components 文件夹, git clone 本仓库到该文件夹下,然后在全局目录,执行

npm i nodemailer --save && npm i @types/nodemailer --save-dev

如果在仓库里面有dist包,你可以这样操作 在你的package.json里面添加下面👇的依赖

"dependencies":{
    "midwayjs_mailer": "https://gitee.com/onlymry_admin/midwayjs_mailer.git"
}

然后执行

yarn install

如果咩有,那就老老实实按照第一步来吧!

启用组件

在 src/configuration.ts 中引入组件。

import * as mailer from './components/mailer';

@Configuration({
  imports: [
    // ...other components
    mailer,
  ],
})
export class MainConfiguration {}

调用服务

一般情况配合@midwayjs/captcha组件使用

$ npm i @midwayjs/captcha@3 --save

记得引入验证码组件,下面是使用案例👇👇👇👇👇

import { Body, Controller, Inject, Post } from '@midwayjs/core';
import { CaptchaService } from '@midwayjs/captcha';
import { MailerService } from '../../components/mailer';

@Controller('/email')
export class EmailController {
  @Inject()
  mailerService: MailerService;

  @Inject()
  captchaService: CaptchaService;

  @Post('/send')
  @Validate()
  async send(@Body() body) {
    const { id, text: code } = await this.captchaService.text({
      type: 'number',
    });
    const data = {
      to: body.email,
      subject: '验证码',
      text: '',
      html: `<code
                  style="font-size: 24px;
                          color:#fff;
                          font-weight: bold;
                          background:#09f;
            ">${code}</code>
            <div>5分钟内有效,请不要告诉他人哦!</div>
            `,
    };
    const message = await this.mailerService.send(data);
    return {
      codeKey: id,
      ...message,
    };
  }
}

可用配置 config.default.ts/config.prod.ts

import { MidwayConfig } from '@midwayjs/core';
export default {
  mailer: {
    service: '', //163、qq等
    prefix: '', //邮件前缀 例如:【xxx平台】
    auth: {
      user: '', //邮箱 [email protected]
      pass: '', //邮箱密码或授权码 建议使用授权码
    },
  },
} as MidwayConfig;

// 网易邮箱
export default {
  mailer :{
    host: 'smtp.sina.com', //163、qq等
    secureConnection: true,
    prefix: '', //邮件前缀
    auth: {
      user: '', //邮箱
      pass: '', //邮箱密码或授权码
    },
  }
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published