1.0.2 • Published 3 years ago

stone-email v1.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

使用方法

$ npm install stone-email -S 

引入模块

在configuration.ts中

import { App, Configuration } from '@midwayjs/decorator';
import { ILifeCycle } from '@midwayjs/core';
import { Application } from 'egg';
import { join } from 'path';
import * as email from 'stone-email';

@Configuration({
  imports: [email],
  importConfigs: [join(__dirname, './config')],
})
export class ContainerLifeCycle implements ILifeCycle {
  @App()
  app: Application;

  async onReady() {
    
  }
}

配置

在config.default.ts中配置:

export const email = {
  transporter: {
    service: 'smtp.163.com',
    host: 'smtp.163.com',
    secureConnection: true,
    port: 465,
    auth: {
      user: `xxxx`, // 账号
      pass: 'xxxx'  // 密码
    }
  },
  defaults: {}
}

调用方法

import { Controller, Inject, Post, Provide } from "@midwayjs/decorator";
import { BaseController } from '../common/baseController';
import { EmailService } from 'stone-email'

/**
 * 邮件发送控制器
 * @author: stone-jin
 * @date: 2021-08-28 18:40:01
 */
@Provide()
@Controller("/api/email")
export class EmailController extends BaseController{

  @Inject()
  emailService: EmailService;

  /**
   * 发送邮件
   * @author: stone-jin
   * @date: 2021-08-28 18:39:54
   */
  @Post("/sendEmail")
  async sendEmail() {
    let data = this.ctx.request.body;
    await this.emailService.sendMail({
      from: '"stone-jin"<hzjinbing@163.com>',
      to: data.to,
      subject: data.title,
      text: data.content
    });
    return this.ok(true)
  }

  /**
   * 发送邮件(html)
   * @author: stone-jin
   * @date: 2021-08-28 18:40:42
   */
  @Post("/sendEmailHtml")
  async sendEmailHtml() {
    let data = this.ctx.request.body;
    await this.emailService.sendMail({
      from: '"stone-jin"<hzjinbing@163.com>',
      to: data.to,
      subject: data.title,
      html: data.content
    });
    return this.ok(true)
  }
}
1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago