Files
certd/packages/ui/certd-server/src/modules/basic/service/email-service.ts
T

100 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-07-15 00:30:33 +08:00
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
2023-06-25 15:30:18 +08:00
import type { EmailSend } from '@certd/pipeline';
2024-11-04 16:39:02 +08:00
import { IEmailService } from '@certd/pipeline';
import { logger } from '@certd/basic';
import { isPlus } from '@certd/plus-core';
2023-06-25 15:30:18 +08:00
import nodemailer from 'nodemailer';
import type SMTPConnection from 'nodemailer/lib/smtp-connection';
2024-07-15 00:30:33 +08:00
import { UserSettingsService } from '../../mine/service/user-settings-service.js';
2024-10-11 03:40:24 +08:00
import { PlusService, SysSettingsService } from '@certd/lib-server';
import { getEmailSettings } from '../../sys/settings/fix.js';
2023-06-25 15:30:18 +08:00
export type EmailConfig = {
host: string;
port: number;
auth: {
user: string;
pass: string;
};
secure: boolean; // use TLS
tls: {
// do not fail on invalid certs
rejectUnauthorized: boolean;
};
sender: string;
2024-08-23 11:35:34 +08:00
usePlus?: boolean;
2023-06-25 15:30:18 +08:00
} & SMTPConnection.Options;
@Provide()
@Scope(ScopeEnum.Singleton)
export class EmailService implements IEmailService {
@Inject()
settingsService: UserSettingsService;
@Inject()
sysSettingsService: SysSettingsService;
2024-08-23 11:35:34 +08:00
@Inject()
plusService: PlusService;
async sendByPlus(email: EmailSend) {
if (!isPlus()) {
throw new Error('plus not enabled');
}
/**
* userId: number;
* subject: string;
* content: string;
* receivers: string[];
*/
2024-11-07 02:22:14 +08:00
await this.plusService.sendEmail(email);
2024-08-23 11:35:34 +08:00
}
2023-06-25 15:30:18 +08:00
/**
*/
async send(email: EmailSend) {
logger.info('sendEmail', email);
2024-11-25 23:48:04 +08:00
if (!email.receivers || email.receivers.length === 0) {
throw new Error('收件人不能为空');
}
2024-10-11 03:40:24 +08:00
const emailConf = await getEmailSettings(this.sysSettingsService, this.settingsService);
2023-06-25 15:30:18 +08:00
2024-10-11 03:40:24 +08:00
if (!emailConf.host && emailConf.usePlus == null) {
2024-08-23 11:35:34 +08:00
if (isPlus()) {
//自动使用plus发邮件
return await this.sendByPlus(email);
}
2024-11-26 01:39:19 +08:00
throw new Error('邮件服务器还未设置');
2023-06-25 15:30:18 +08:00
}
if (emailConf.usePlus && isPlus()) {
2024-08-23 11:35:34 +08:00
return await this.sendByPlus(email);
}
await this.sendByCustom(emailConf, email);
2024-08-23 11:35:34 +08:00
logger.info('sendEmail complete: ', email);
}
private async sendByCustom(emailConfig: EmailConfig, email: EmailSend) {
2023-06-25 15:30:18 +08:00
const transporter = nodemailer.createTransport(emailConfig);
const mailOptions = {
from: emailConfig.sender,
to: email.receivers.join(', '), // list of receivers
subject: email.subject,
text: email.content,
};
await transporter.sendMail(mailOptions);
}
async test(userId: number, receiver: string) {
await this.send({
receivers: [receiver],
subject: '测试邮件,from certd',
content: '测试邮件,from certd',
});
}
}