import { TFunction } from "next-i18next"; import nodemailer from "nodemailer"; import { getErrorFromUnknown } from "@calcom/lib/errors"; import { serverConfig } from "@calcom/lib/serverConfig"; import { emailHead, linkIcon, emailBodyLogo } from "./common"; export type TeamInvite = { language: TFunction; from: string; to: string; teamName: string; joinLink: string; }; export default class TeamInviteEmail { teamInviteEvent: TeamInvite; constructor(teamInviteEvent: TeamInvite) { this.teamInviteEvent = teamInviteEvent; } public sendEmail() { 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 getNodeMailerPayload(): Record { return { to: this.teamInviteEvent.to, from: `Cal.com <${this.getMailerOptions().from}>`, subject: this.teamInviteEvent.language("user_invited_you", { user: this.teamInviteEvent.from, team: this.teamInviteEvent.teamName, }), html: this.getHtmlBody(), text: this.getTextBody(), }; } protected printNodeMailerError(error: Error): void { console.error("SEND_TEAM_INVITE_EMAIL_ERROR", this.teamInviteEvent.to, error); } protected getTextBody(): string { return ""; } protected getHtmlBody(): string { const headerContent = this.teamInviteEvent.language("user_invited_you", { user: this.teamInviteEvent.from, team: this.teamInviteEvent.teamName, }); return ` ${emailHead(headerContent)}
${emailBodyLogo()}

${this.teamInviteEvent.language("user_invited_you", { user: this.teamInviteEvent.from, team: this.teamInviteEvent.teamName, })}!

${this.teamInviteEvent.language( "calcom_explained" )}

${this.teamInviteEvent.language( "accept_invitation" )}

${this.teamInviteEvent.language( "have_any_questions" )} ${this.teamInviteEvent.language( "contact_our_support_team" )}

`; } }