import { TFunction } from "next-i18next"; import nodemailer from "nodemailer"; import { getErrorFromUnknown } from "@lib/errors"; import { serverConfig } from "../serverConfig"; export type Invitation = { language: TFunction; from?: string; toEmail: string; teamName: string; token?: string; }; type EmailProvider = { from: string; transport: any; }; export function createInvitationEmail(data: Invitation) { const provider = { transport: serverConfig.transport, from: serverConfig.from, } as EmailProvider; return sendEmail(data, provider); } const sendEmail = (invitation: Invitation, provider: EmailProvider): Promise => new Promise((resolve, reject) => { const { transport, from } = provider; const { language: t } = invitation; const invitationHtml = html(invitation); nodemailer.createTransport(transport).sendMail( { from: `Cal.com <${from}>`, to: invitation.toEmail, subject: invitation.from ? t("user_invited_you", { user: invitation.from, teamName: invitation.teamName }) : t("you_have_been_invited", { teamName: invitation.teamName }), html: invitationHtml, text: text(invitationHtml), }, (_err) => { if (_err) { const err = getErrorFromUnknown(_err); console.error("SEND_INVITATION_NOTIFICATION_ERROR", invitation.toEmail, err); reject(err); return; } return resolve(); } ); }); export function html(invitation: Invitation): string { const { language: t } = invitation; let url: string = process.env.BASE_URL + "/settings/teams"; if (invitation.token) { url = `${process.env.BASE_URL}/auth/signup?token=${invitation.token}&callbackUrl=${url}`; } return ( `
${t("hi")},

` + (invitation.from ? t("user_invited_you", { user: invitation.from, teamName: invitation.teamName }) : t("you_have_been_invited", { teamName: invitation.teamName })) + `


${t("request_another_invitation_email", { toEmail: invitation.toEmail })}
` ); } // just strip all HTML and convert
to \n export function text(htmlStr: string): string { return htmlStr.replace("
", "\n").replace(/<[^>]+>/g, ""); }