import nodemailer from "nodemailer"; import { serverConfig } from "../serverConfig"; export type Invitation = { 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 invitationHtml = html(invitation); nodemailer.createTransport(transport).sendMail( { from: `Cal.com <${from}>`, to: invitation.toEmail, subject: (invitation.from ? invitation.from + " invited you" : "You have been invited") + ` to join ${invitation.teamName}`, html: invitationHtml, text: text(invitationHtml), }, (error) => { if (error) { console.error("SEND_INVITATION_NOTIFICATION_ERROR", invitation.toEmail, error); return reject(new Error(error)); } return resolve(); } ); }); export function html(invitation: Invitation): string { 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 ( `
Hi,

` + (invitation.from ? invitation.from + " invited you" : "You have been invited") + ` to join the team "${invitation.teamName}" in Cal.com.


If you prefer not to use "${invitation.toEmail}" as your Cal.com email or already have a Cal.com account, please request another invitation to that email.
` ); } // just strip all HTML and convert
to \n export function text(htmlStr: string): string { return htmlStr.replace("
", "\n").replace(/<[^>]+>/g, ""); }