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';
|
2024-12-04 22:27:48 +08:00
|
|
|
import { isComm, isPlus } from '@certd/plus-core';
|
2024-11-04 16:39:02 +08:00
|
|
|
|
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-12-04 22:27:48 +08:00
|
|
|
import { PlusService, SysSettingsService, SysSiteInfo } from '@certd/lib-server';
|
2024-10-11 03:40:24 +08:00
|
|
|
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()
|
2024-06-16 00:20:02 +08:00
|
|
|
settingsService: UserSettingsService;
|
2024-10-11 02:54:42 +08:00
|
|
|
|
|
|
|
|
@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) {
|
2024-10-11 02:54:42 +08:00
|
|
|
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
|
|
|
}
|
2024-10-11 02:54:42 +08:00
|
|
|
|
|
|
|
|
if (emailConf.usePlus && isPlus()) {
|
2024-08-23 11:35:34 +08:00
|
|
|
return await this.sendByPlus(email);
|
|
|
|
|
}
|
2024-10-11 02:54:42 +08:00
|
|
|
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);
|
2024-12-04 22:27:48 +08:00
|
|
|
|
|
|
|
|
let sysTitle = 'Certd';
|
|
|
|
|
if (isComm()) {
|
|
|
|
|
const siteInfo = await this.sysSettingsService.getSetting<SysSiteInfo>(SysSiteInfo);
|
|
|
|
|
if (siteInfo) {
|
|
|
|
|
sysTitle = siteInfo.title || sysTitle;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-11 11:37:52 +08:00
|
|
|
let subject = email.subject;
|
|
|
|
|
if (!subject.startsWith(`【${sysTitle}】`)) {
|
|
|
|
|
subject = `【${sysTitle}】${subject}`;
|
|
|
|
|
}
|
2023-06-25 15:30:18 +08:00
|
|
|
const mailOptions = {
|
2024-12-04 22:27:48 +08:00
|
|
|
from: `${sysTitle} <${emailConfig.sender}>`,
|
2023-06-25 15:30:18 +08:00
|
|
|
to: email.receivers.join(', '), // list of receivers
|
2024-12-11 11:37:52 +08:00
|
|
|
subject: subject,
|
2023-06-25 15:30:18 +08:00
|
|
|
text: email.content,
|
|
|
|
|
};
|
|
|
|
|
await transporter.sendMail(mailOptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async test(userId: number, receiver: string) {
|
|
|
|
|
await this.send({
|
|
|
|
|
receivers: [receiver],
|
|
|
|
|
subject: '测试邮件,from certd',
|
|
|
|
|
content: '测试邮件,from certd',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|