import { serverConfig } from "../serverConfig";
import nodemailer from "nodemailer";
export default function createInvitationEmail(data: any, options: any = {}) {
return sendEmail(data, {
provider: {
transport: serverConfig.transport,
from: serverConfig.from,
},
...options,
});
}
const sendEmail = (invitation: any, { provider }) =>
new Promise((resolve, reject) => {
const { transport, from } = provider;
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: html(invitation),
text: text(invitation),
},
(error) => {
if (error) {
console.error("SEND_INVITATION_NOTIFICATION_ERROR", invitation.toEmail, error);
return reject(new Error(error));
}
return resolve();
}
);
});
const html = (invitation: any) => {
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
const text = (evt: any) =>
html(evt)
.replace("
", "\n")
.replace(/<[^>]+>/g, "");