2022-05-13 12:34:54 +00:00
|
|
|
import nodemailer from "nodemailer";
|
|
|
|
|
2022-06-28 20:40:58 +00:00
|
|
|
import dayjs, { Dayjs } from "@calcom/dayjs";
|
2022-05-13 12:34:54 +00:00
|
|
|
import { getErrorFromUnknown } from "@calcom/lib/errors";
|
|
|
|
import { serverConfig } from "@calcom/lib/serverConfig";
|
|
|
|
|
2022-08-26 18:44:02 +00:00
|
|
|
declare let global: {
|
|
|
|
E2E_EMAILS?: Record<string, unknown>[];
|
|
|
|
};
|
|
|
|
|
2022-05-13 12:34:54 +00:00
|
|
|
export default class BaseEmail {
|
2022-05-17 20:43:27 +00:00
|
|
|
name = "";
|
2022-05-13 12:34:54 +00:00
|
|
|
|
2022-06-06 17:49:56 +00:00
|
|
|
protected getTimezone() {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getRecipientTime(time: string): Dayjs;
|
|
|
|
protected getRecipientTime(time: string, format: string): string;
|
|
|
|
protected getRecipientTime(time: string, format?: string) {
|
|
|
|
const date = dayjs(time).tz(this.getTimezone());
|
|
|
|
if (typeof format === "string") return date.format(format);
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
|
2022-05-13 12:34:54 +00:00
|
|
|
protected getNodeMailerPayload(): Record<string, unknown> {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
public sendEmail() {
|
2022-08-26 18:44:02 +00:00
|
|
|
if (process.env.NEXT_PUBLIC_IS_E2E) {
|
|
|
|
global.E2E_EMAILS = global.E2E_EMAILS || [];
|
|
|
|
global.E2E_EMAILS.push(this.getNodeMailerPayload());
|
|
|
|
console.log("Skipped Sending Email");
|
|
|
|
return new Promise((r) => r("Skipped sendEmail for E2E"));
|
|
|
|
}
|
2022-05-13 12:34:54 +00:00
|
|
|
new Promise((resolve, reject) =>
|
|
|
|
nodemailer
|
|
|
|
.createTransport(this.getMailerOptions().transport)
|
|
|
|
.sendMail(this.getNodeMailerPayload(), (_err, info) => {
|
|
|
|
if (_err) {
|
|
|
|
const err = getErrorFromUnknown(_err);
|
|
|
|
this.printNodeMailerError(err);
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(info);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
).catch((e) => console.error("sendEmail", e));
|
|
|
|
return new Promise((resolve) => resolve("send mail async"));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getMailerOptions() {
|
|
|
|
return {
|
|
|
|
transport: serverConfig.transport,
|
|
|
|
from: serverConfig.from,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected printNodeMailerError(error: Error): void {
|
|
|
|
/** Don't clog the logs with unsent emails in E2E */
|
|
|
|
if (process.env.NEXT_PUBLIC_IS_E2E) return;
|
|
|
|
console.error(`${this.name}_ERROR`, error);
|
|
|
|
}
|
|
|
|
}
|