mirror of
https://github.com/docmost/docmost.git
synced 2025-11-20 19:01:12 +10:00
email integration
* Nest email module with smtp, postmark and console log drivers * react-email package
This commit is contained in:
3
apps/server/src/integrations/mail/drivers/index.ts
Normal file
3
apps/server/src/integrations/mail/drivers/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export { SmtpDriver } from './smtp.driver';
|
||||
export { PostmarkDriver } from './postmark.driver';
|
||||
export { LogDriver } from './log.driver';
|
||||
@ -0,0 +1,5 @@
|
||||
import { MailMessage } from '../../interfaces/mail.message';
|
||||
|
||||
export interface MailDriver {
|
||||
sendMail(message: MailMessage): Promise<void>;
|
||||
}
|
||||
18
apps/server/src/integrations/mail/drivers/log.driver.ts
Normal file
18
apps/server/src/integrations/mail/drivers/log.driver.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { MailDriver } from './interfaces/mail-driver.interface';
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { MailMessage } from '../interfaces/mail.message';
|
||||
import { mailLogName } from '../mail.utils';
|
||||
|
||||
export class LogDriver implements MailDriver {
|
||||
private readonly logger = new Logger(mailLogName(LogDriver.name));
|
||||
|
||||
async sendMail(message: MailMessage): Promise<void> {
|
||||
const mailLog = {
|
||||
to: message.to,
|
||||
subject: message.subject,
|
||||
text: message.text,
|
||||
};
|
||||
|
||||
this.logger.log(`Logged mail: ${JSON.stringify(mailLog)}`);
|
||||
}
|
||||
}
|
||||
30
apps/server/src/integrations/mail/drivers/postmark.driver.ts
Normal file
30
apps/server/src/integrations/mail/drivers/postmark.driver.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import { MailDriver } from './interfaces/mail-driver.interface';
|
||||
import { PostmarkConfig } from '../interfaces';
|
||||
import { ServerClient } from 'postmark';
|
||||
import { MailMessage } from '../interfaces/mail.message';
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { mailLogName } from '../mail.utils';
|
||||
|
||||
export class PostmarkDriver implements MailDriver {
|
||||
private readonly logger = new Logger(mailLogName(PostmarkDriver.name));
|
||||
private readonly postmarkClient: ServerClient;
|
||||
|
||||
constructor(config: PostmarkConfig) {
|
||||
this.postmarkClient = new ServerClient(config.postmarkToken);
|
||||
}
|
||||
|
||||
async sendMail(message: MailMessage): Promise<void> {
|
||||
try {
|
||||
await this.postmarkClient.sendEmail({
|
||||
From: message.from,
|
||||
To: message.to,
|
||||
Subject: message.subject,
|
||||
TextBody: message.text,
|
||||
HtmlBody: message.html,
|
||||
});
|
||||
this.logger.debug(`Sent mail to ${message.to}`);
|
||||
} catch (err) {
|
||||
this.logger.warn(`Failed to send mail to ${message.to}: ${err}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
32
apps/server/src/integrations/mail/drivers/smtp.driver.ts
Normal file
32
apps/server/src/integrations/mail/drivers/smtp.driver.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { MailDriver } from './interfaces/mail-driver.interface';
|
||||
import { SMTPConfig } from '../interfaces';
|
||||
import { Transporter } from 'nodemailer';
|
||||
import * as nodemailer from 'nodemailer';
|
||||
import { MailMessage } from '../interfaces/mail.message';
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { mailLogName } from '../mail.utils';
|
||||
|
||||
export class SmtpDriver implements MailDriver {
|
||||
private readonly logger = new Logger(mailLogName(SmtpDriver.name));
|
||||
private readonly transporter: Transporter;
|
||||
|
||||
constructor(config: SMTPConfig) {
|
||||
this.transporter = nodemailer.createTransport(config);
|
||||
}
|
||||
|
||||
async sendMail(message: MailMessage): Promise<void> {
|
||||
try {
|
||||
await this.transporter.sendMail({
|
||||
from: message.from,
|
||||
to: message.to,
|
||||
subject: message.subject,
|
||||
text: message.text,
|
||||
html: message.html,
|
||||
});
|
||||
|
||||
this.logger.debug(`Sent mail to ${message.to}`);
|
||||
} catch (err) {
|
||||
this.logger.warn(`Failed to send mail to ${message.to}: ${err}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user