Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for cid based attachment in email #4952

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libs/shared/src/types/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export interface IAttachmentOptions {
file: Buffer;
name?: string;
channels?: ChannelTypeEnum[];
cid?: string;
disposition?: string;
}

export interface IEmailOptions {
Expand Down
2 changes: 2 additions & 0 deletions packages/stateless/src/lib/template/template.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export interface IAttachmentOptions {
file: Buffer | null;
name?: string;
channels?: ChannelTypeEnum[];
cid?: string;
disposition?: string;
}

export interface IAttachmentOptionsExtended extends IAttachmentOptions {
Expand Down
31 changes: 24 additions & 7 deletions providers/sendgrid/src/lib/sendgrid.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import {
ICheckIntegrationResponse,
CheckIntegrationResponseEnum,
IEmailEventBody,
IAttachmentOptions,
} from '@novu/stateless';

import { MailDataRequired, MailService } from '@sendgrid/mail';

type AttachmentJSON = MailDataRequired['attachments'][0];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea why but the AttachmentJSON is not exported from @sendgrid/mail, created this workaround so we won't use any in the code, and without importing other libs or creating internal types that can become stale.


export class SendgridEmailProvider implements IEmailProvider {
id = 'sendgrid';
channelType = ChannelTypeEnum.EMAIL as ChannelTypeEnum.EMAIL;
Expand Down Expand Up @@ -74,6 +77,26 @@ export class SendgridEmailProvider implements IEmailProvider {
delete options.customData?.dynamicTemplateData;
delete options.customData?.templateId;

const attachments = options.attachments?.map(
(attachment: IAttachmentOptions) => {
const attachmentJson: Partial<AttachmentJSON> = {
djabarovgeorge marked this conversation as resolved.
Show resolved Hide resolved
content: attachment.file.toString('base64'),
filename: attachment.name,
type: attachment.mime,
};

if (attachment?.cid) {
attachmentJson.contentId = attachment?.cid;
}

if (attachment?.disposition) {
attachmentJson.disposition = attachment?.disposition;
}

return attachmentJson as AttachmentJSON;
}
);

const mailData: Partial<MailDataRequired> = {
from: {
email: options.from || this.config.from,
Expand All @@ -95,13 +118,7 @@ export class SendgridEmailProvider implements IEmailProvider {
novuSubscriberId: options.notificationDetails?.subscriberId,
...options.customData,
},
attachments: options.attachments?.map((attachment) => {
return {
content: attachment.file.toString('base64'),
filename: attachment.name,
type: attachment.mime,
};
}),
attachments: attachments,
personalizations: [
{
to: options.to.map((email) => ({ email })),
Expand Down