feat: 邮件通知

This commit is contained in:
xiaojunnuo
2023-06-25 15:30:18 +08:00
parent 64afebecd4
commit 937e3fac19
32 changed files with 790 additions and 88 deletions

View File

@@ -0,0 +1,60 @@
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
import type { EmailSend } from '@certd/pipeline';
import { IEmailService } from '@certd/pipeline';
import nodemailer from 'nodemailer';
import { SettingsService } from '../../system/service/settings-service';
import type SMTPConnection from 'nodemailer/lib/smtp-connection';
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;
} & SMTPConnection.Options;
@Provide()
@Scope(ScopeEnum.Singleton)
export class EmailService implements IEmailService {
@Inject()
settingsService: SettingsService;
/**
*/
async send(email: EmailSend) {
console.log('sendEmail', email);
const emailConfigEntity = await this.settingsService.getByKey(
'email',
email.userId
);
if (emailConfigEntity == null || !emailConfigEntity.setting) {
throw new Error('email settings 未设置');
}
const emailConfig = JSON.parse(emailConfigEntity.setting) as EmailConfig;
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);
console.log('sendEmail success', email);
}
async test(userId: number, receiver: string) {
await this.send({
userId,
receivers: [receiver],
subject: '测试邮件,from certd',
content: '测试邮件,from certd',
});
}
}