diff --git a/apps/web/pages/api/documents/[id]/send.ts b/apps/web/pages/api/documents/[id]/send.ts index 42d22a5df..3b84e15b9 100644 --- a/apps/web/pages/api/documents/[id]/send.ts +++ b/apps/web/pages/api/documents/[id]/send.ts @@ -42,17 +42,20 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) { const recipients = prisma.recipient.findMany({ where: { documentId: +documentId, - sendStatus: SendStatus.NOT_SENT, // TODO REDO AFTER DEBUG + // sendStatus: SendStatus.NOT_SENT, // TODO REDO AFTER DEBUG }, }); + (await recipients).forEach(async (recipient) => { + await sendSigningRequest(recipient, document) + .then(() => { + res.status(200).end(); + }) + .catch((err) => { + return res.status(502).end("Coud not send request for signing."); + }); + }); // todo check if recipient has an account and show them in their inbox or something - (await recipients).forEach(async (recipient) => { - await sendSigningRequest(recipient, document); - }); - - // todo way better error handling - return res.status(200).end(); } export default defaultHandler({ diff --git a/packages/lib/mail/index.ts b/packages/lib/mail/index.ts index 0fe0d7963..5693dfb53 100644 --- a/packages/lib/mail/index.ts +++ b/packages/lib/mail/index.ts @@ -1,2 +1,3 @@ export { sendSigningRequest as sendSigningRequest } from "./sendSigningRequest"; export { sendSignedMail } from "./sendSignedMail"; +export { transactionEmailTemplate } from "./transactionEmailTemplate" \ No newline at end of file diff --git a/packages/lib/mail/sendMail.ts b/packages/lib/mail/sendMail.ts index b7d27f2c2..7c32d1a45 100644 --- a/packages/lib/mail/sendMail.ts +++ b/packages/lib/mail/sendMail.ts @@ -1,24 +1,23 @@ import nodemailer from "nodemailer"; import nodemailerSendgrid from "nodemailer-sendgrid"; -export const sendMail = async ( - to: string, - subject: string, - htmlFormattedMessage: string -) => { +export const sendMail = async (to: string, subject: string, body: string) => { if (!process.env.SENDGRID_API_KEY) throw new Error("Sendgrid API Key not set."); - const transport = nodemailer.createTransport( + const transport = await nodemailer.createTransport( nodemailerSendgrid({ apiKey: process.env.SENDGRID_API_KEY || "", }) ); - - await transport.sendMail({ - from: process.env.MAIL_FROM, - to: to, - subject: subject, - html: htmlFormattedMessage, - }); + await transport + .sendMail({ + from: process.env.MAIL_FROM, + to: to, + subject: subject, + html: body, + }) + .catch((err) => { + throw err; + }); }; diff --git a/packages/lib/mail/sendSignedMail.ts b/packages/lib/mail/sendSignedMail.ts index 7fca9a38d..f6798e7b4 100644 --- a/packages/lib/mail/sendSignedMail.ts +++ b/packages/lib/mail/sendSignedMail.ts @@ -1,50 +1,20 @@ import { sendMail } from "./sendMail"; import { NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib/constants"; +import { transactionEmailTemplate } from "@documenso/lib/mail"; export const sendSignedMail = async (recipient: any, document: any) => { // todo check if recipient has an account await sendMail( document.User.email, `${recipient.email} signed "${document.title}"`, - ` -
-
- Documenso Logo - ${document.User.name || recipient.email} has signed your document ${ - document.title - }. -

- - View Document - -

-
- Click the button to view ${document.title}.
- If you have questions about this document, your should ask ${ - document.User.name - }. -
-
- -
-
- Do not forward. -
- This email contains a link to a secure document. Keep it secret and do not forward this email. -
-
- Need help? -
- Contact us at hi@documenso.com -
-
-
- Easy and beautiful document signing by Documenso. -
-
- ` + transactionEmailTemplate( + `${document.User.name || recipient.email} has signed your document ${ + document.title + }`, + document, + recipient, + `${NEXT_PUBLIC_WEBAPP_URL}/documents/${document.id}`, + `View Document` + ) ); }; diff --git a/packages/lib/mail/sendSigningRequest.ts b/packages/lib/mail/sendSigningRequest.ts index 3a256f382..4ab257d79 100644 --- a/packages/lib/mail/sendSigningRequest.ts +++ b/packages/lib/mail/sendSigningRequest.ts @@ -2,47 +2,24 @@ import prisma from "@documenso/prisma"; import { sendMail } from "./sendMail"; import { SendStatus, DocumentStatus } from "@prisma/client"; import { NEXT_PUBLIC_WEBAPP_URL } from "../constants"; +import { transactionEmailTemplate } from "@documenso/lib/mail"; export const sendSigningRequest = async (recipient: any, document: any) => { // todo errror handling await sendMail( document.User.email, `Please sign ${document.title}`, - ` -
-
- Documenso Logo - ${document.User.name} (${document.User.email}) has sent you a document to sign. -

- - Sign Document - -

-
- Click the button to view and sign ${document.title}.
- If you have questions about this document, your should ask ${document.User.name}. -
-
- -
-
- Do not forward. -
- This email contains a link to a secure document. Keep it secret and do not forward this email. -
-
- Need help? -
- Contact us at hi@documenso.com -
-
-
- Easy and beautiful document signing by Documenso. -
-
- ` - ); - + transactionEmailTemplate( + `${document.User.name} (${document.User.email}) has sent you a document to sign. `, + document, + recipient, + `${NEXT_PUBLIC_WEBAPP_URL}/documents/${document.id}/sign?token=${recipient.token}`, + `Sign Document` + ) + ).catch((err) => { + console.log("catch 1"); + throw err; + }); await prisma.recipient.update({ where: { id: recipient.id, diff --git a/packages/lib/mail/transactionEmailTemplate.ts b/packages/lib/mail/transactionEmailTemplate.ts new file mode 100644 index 000000000..d65a0f750 --- /dev/null +++ b/packages/lib/mail/transactionEmailTemplate.ts @@ -0,0 +1,51 @@ +import { NEXT_PUBLIC_WEBAPP_URL } from "../constants"; +import { Document as PrismaDocument } from "@prisma/client"; + +export const transactionEmailTemplate = ( + message: string, + document: any, + recipient: any, + ctaLink: string, + ctaLabel: string +) => { + const html = ` +
+
+ Documenso Logo + ${message} +

+ + ${ctaLabel} + +

+
+ Click the button to view "${document.title}".
+ If you have questions about this document, you should ask ${document.User.name}. +
+
+ `; + + const footer = ` +
+
+ Do not forward. +
+ This email contains a link to a secure document. Keep it secret and do not forward this email. +
+
+ Need help? +
+ Contact us at hi@documenso.com +
+
+
+ Easy and beautiful document signing by Documenso. +
+
+
+ `; + + return html + footer; +}; + +export default transactionEmailTemplate;