Skip to content

Commit

Permalink
Merge pull request #4952 from novuhq/nv-3185-add-support-for-cid-base…
Browse files Browse the repository at this point in the history
…d-attachment-in-email

Add support for cid based attachment in email
  • Loading branch information
djabarovgeorge authored Dec 17, 2023
2 parents dd71192 + 0c3ac49 commit 0744144
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
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];

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: AttachmentJSON = {
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;
}
);

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

1 comment on commit 0744144

@github-actions
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.