Characteristic

CabloyJS has built-in mail sending module a-mail, which encapsulates the most popular [nodemailer] (https://nodemailer.com/about/), with the following features:

  • more concise configuration and API
  • send mail in queue
  • auto resend when failed
  • configure email account based on scenario
  • Built-in test account for easy development and debugging

Scene & Email Account

CabloyJS configures email accounts based on scenarios to support more flexible business requirements. CabloyJS has two built-in scenarios:

Name Description
system system scene. For example, the authentication module will use the email account of this scenario to send a confirmation email
test test scene. In this scenario, the nodemailer built-in test account is used to send email, and the email URL link is automatically printed to the console, so as to facilitate development and debugging

If you need to add other scenarios, you can directly modify the config of the module

Config

a-mail/backend/src/config/config.js

// scenes
config.scenes = {
  system: {
    transport: {
      host: '',
      port: 0,
      secure: false,
      auth: {
        user: '',
        pass: '',
      },
      logger: false,
      debug: false,
    },
    defaults: {
      from: '',
    },
  },
};
  • transport
Name Description
host domain host or IP of SMTP
port port of SMTP
secure use TLS for secure connection
auth.user username
auth.pass password
logger print log or not
debug print detailed SMTP communication information
  • defaults
Name Description
from field from of email

nodemailer also supports other fields, which can also be set in config. For details, please refer to: SMTP TRANSPORT

API

src/suite-vendor/test-party/modules/test-party/backend/src/controller/test/feat/sendMail.js

async sendMail() {
  // send
  await this.ctx.meta.mail.send({
    scene: 'test',
    message: {
      to: 'test@cabloy.com',
      subject: 'this is a test',
      text: 'message body!',
    },
  });
  // done
  this.ctx.success();
}
Name Description
scene scene
message.to field to
message.subject field subject
message.text field text

message can also be passed to other fields. For details, please refer to: MESSAGE CONFIGURATION