mirror of
https://github.com/documenso/documenso.git
synced 2025-11-15 01:01:49 +10:00
feat: replace template variables with values
Co-authored-by: Mythie <me@lucasjamessmith.me>
This commit is contained in:
@ -3,6 +3,7 @@ import { createElement } from 'react';
|
|||||||
import { mailer } from '@documenso/email/mailer';
|
import { mailer } from '@documenso/email/mailer';
|
||||||
import { render } from '@documenso/email/render';
|
import { render } from '@documenso/email/render';
|
||||||
import { DocumentInviteEmailTemplate } from '@documenso/email/templates/document-invite';
|
import { DocumentInviteEmailTemplate } from '@documenso/email/templates/document-invite';
|
||||||
|
import { renderCustomEmailTemplate } from '@documenso/lib/utils/render-custom-email-template';
|
||||||
import { prisma } from '@documenso/prisma';
|
import { prisma } from '@documenso/prisma';
|
||||||
import { DocumentStatus, SendStatus } from '@documenso/prisma/client';
|
import { DocumentStatus, SendStatus } from '@documenso/prisma/client';
|
||||||
|
|
||||||
@ -47,6 +48,12 @@ export const sendDocument = async ({ documentId, userId }: SendDocumentOptions)
|
|||||||
document.Recipient.map(async (recipient) => {
|
document.Recipient.map(async (recipient) => {
|
||||||
const { email, name } = recipient;
|
const { email, name } = recipient;
|
||||||
|
|
||||||
|
const customEmailTemplate = {
|
||||||
|
'signer.name': name,
|
||||||
|
'signer.email': email,
|
||||||
|
'document.name': document.title,
|
||||||
|
};
|
||||||
|
|
||||||
if (recipient.sendStatus === SendStatus.SENT) {
|
if (recipient.sendStatus === SendStatus.SENT) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -60,7 +67,10 @@ export const sendDocument = async ({ documentId, userId }: SendDocumentOptions)
|
|||||||
inviterEmail: user.email,
|
inviterEmail: user.email,
|
||||||
assetBaseUrl,
|
assetBaseUrl,
|
||||||
signDocumentLink,
|
signDocumentLink,
|
||||||
customBody: customEmail?.customEmailBody || '',
|
customBody: renderCustomEmailTemplate(
|
||||||
|
customEmail?.customEmailBody || '',
|
||||||
|
customEmailTemplate,
|
||||||
|
),
|
||||||
});
|
});
|
||||||
|
|
||||||
await mailer.sendMail({
|
await mailer.sendMail({
|
||||||
@ -73,7 +83,7 @@ export const sendDocument = async ({ documentId, userId }: SendDocumentOptions)
|
|||||||
address: process.env.NEXT_PRIVATE_SMTP_FROM_ADDRESS || 'noreply@documenso.com',
|
address: process.env.NEXT_PRIVATE_SMTP_FROM_ADDRESS || 'noreply@documenso.com',
|
||||||
},
|
},
|
||||||
subject: customEmail?.customEmailSubject
|
subject: customEmail?.customEmailSubject
|
||||||
? customEmail.customEmailSubject
|
? renderCustomEmailTemplate(customEmail.customEmailSubject, customEmailTemplate)
|
||||||
: 'Please sign this document',
|
: 'Please sign this document',
|
||||||
html: render(template),
|
html: render(template),
|
||||||
text: render(template, { plainText: true }),
|
text: render(template, { plainText: true }),
|
||||||
|
|||||||
20
packages/lib/utils/render-custom-email-template.ts
Normal file
20
packages/lib/utils/render-custom-email-template.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
export const renderCustomEmailTemplate = <T extends Record<string, string>>(
|
||||||
|
template: string,
|
||||||
|
variables: T,
|
||||||
|
): string => {
|
||||||
|
let t = template;
|
||||||
|
|
||||||
|
Object.entries(variables).forEach((entry) => {
|
||||||
|
const [key, value] = entry;
|
||||||
|
|
||||||
|
const placeholder = `{${key}}`;
|
||||||
|
|
||||||
|
const re = new RegExp(placeholder, 'g');
|
||||||
|
|
||||||
|
if (Object.prototype.hasOwnProperty.call(variables, key)) {
|
||||||
|
t = t.replace(re, String(value));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return t;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user