Skip to content

Commit

Permalink
fix: senderName and subject override for email providers (#4903)
Browse files Browse the repository at this point in the history
* fix: sendername and subject override for email providers

* fix: add sendername in return of compile email

* fix: name mistake in sendgrid

* fix: add sendername in plunk and netcore

* fix: netcore and plunk test
  • Loading branch information
jainpawan21 authored Nov 28, 2023
1 parent 4e2c8a2 commit 63f7fe8
Show file tree
Hide file tree
Showing 20 changed files with 47 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ export class SendMessageEmail extends SendMessageBase {
let html;
let subject = step?.template?.subject || '';
let content;
let senderName;

const payload = {
senderName: step.template.senderName,
subject,
preheader: step.template.preheader,
content: step.template.content,
Expand Down Expand Up @@ -203,7 +205,7 @@ export class SendMessageEmail extends SendMessageBase {
}

try {
({ html, content, subject } = await this.compileEmailTemplateUsecase.execute(
({ html, content, subject, senderName } = await this.compileEmailTemplateUsecase.execute(
CompileEmailTemplateCommand.create({
environmentId: command.environmentId,
organizationId: command.organizationId,
Expand Down Expand Up @@ -272,6 +274,7 @@ export class SendMessageEmail extends SendMessageBase {
html,
from: integration?.credentials.from || '[email protected]',
attachments,
senderName,
id: message._id,
replyTo: replyToAddress,
notificationDetails: {
Expand All @@ -292,13 +295,7 @@ export class SendMessageEmail extends SendMessageBase {
}

if (email && integration) {
await this.sendMessage(
integration,
mailData,
message,
command,
overrides?.senderName || step.template.senderName
);
await this.sendMessage(integration, mailData, message, command);

return;
}
Expand Down Expand Up @@ -433,11 +430,10 @@ export class SendMessageEmail extends SendMessageBase {
integration: IntegrationEntity,
mailData: IEmailOptions,
message: MessageEntity,
command: SendMessageCommand,
senderName?: string
command: SendMessageCommand
) {
const mailFactory = new MailFactory();
const mailHandler = mailFactory.getHandler(this.buildFactoryIntegration(integration, senderName), mailData.from);
const mailHandler = mailFactory.getHandler(this.buildFactoryIntegration(integration), mailData.from);

try {
const result = await mailHandler.send(mailData);
Expand Down Expand Up @@ -547,7 +543,6 @@ export class SendMessageEmail extends SendMessageBase {
...integration,
credentials: {
...integration.credentials,
senderName: senderName && senderName.length > 0 ? senderName : integration.credentials.senderName,
},
providerId: integration.providerId,
};
Expand All @@ -570,6 +565,8 @@ export const createMailData = (options: IEmailOptions, overrides: Record<string,
cc: overrides?.cc || [],
bcc: overrides?.bcc || [],
...ipPoolName,
senderName: overrides?.senderName || options.senderName,
subject: overrides?.subject || options.subject,
customData: overrides?.customData || {},
};
};
Expand Down
1 change: 1 addition & 0 deletions libs/shared/src/types/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface IEmailOptions {
notificationDetails?: any;
ipPoolName?: string;
customData?: Record<string, any>;
senderName?: string;
}

export interface ITriggerPayload {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChannelTypeEnum } from '@novu/shared';
import { ChannelTypeEnum, ICredentials } from '@novu/shared';
import { NetCoreProvider } from '@novu/netcore';
import { BaseHandler } from './base.handler';

Expand All @@ -7,10 +7,11 @@ export class NetCoreHandler extends BaseHandler {
super('netcore', ChannelTypeEnum.EMAIL);
}

buildProvider(credentials, from?: string) {
const config: { apiKey: string; from: string } = {
buildProvider(credentials: ICredentials, from?: string) {
const config: { apiKey: string; from: string; senderName: string } = {
apiKey: credentials.apiKey,
from: from as string,
senderName: credentials.senderName,
};

this.provider = new NetCoreProvider(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ export class PlunkHandler extends BaseHandler {
}

buildProvider(credentials: ICredentials) {
const config: { apiKey: string } = {
const config: { apiKey: string; senderName: string } = {
apiKey: credentials.apiKey,
senderName: credentials.senderName,
};

this.provider = new PlunkEmailProvider(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class CompileEmailTemplate {
}

let subject = '';
let senderName = '';
let senderName;
const content: string | IEmailBlock[] = command.content;
let preheader = command.preheader;

Expand Down
1 change: 1 addition & 0 deletions packages/stateless/src/lib/provider/provider.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface IEmailOptions {
notificationDetails?: any;
ipPoolName?: string;
customData?: Record<string, any>;
senderName?: string;
}

export interface ISmsOptions {
Expand Down
2 changes: 1 addition & 1 deletion providers/infobip/src/lib/infobip.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class InfobipEmailProvider implements IEmailProvider {
): Promise<ISendMessageSuccessResponse> {
const infobipResponse = await this.infobipClient.channels.email.send({
to: options.to,
from: this.config.from || options.from,
from: options.from || this.config.from,
subject: options.subject,
text: options.text,
html: options.html,
Expand Down
3 changes: 2 additions & 1 deletion providers/mailersend/src/lib/mailersend.provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CheckIntegrationResponseEnum } from '@novu/stateless';

const mockConfig = {
apiKey: 'SG.1234',
senderName: 'Novu Team',
};

const mockNovuMessage = {
Expand Down Expand Up @@ -72,7 +73,7 @@ test('should trigger mailerSend correctly', async () => {
expect(spy).toBeCalledWith('/email', {
method: 'POST',
body: {
from: { email: mockNovuMessage.from, name: undefined },
from: { email: mockNovuMessage.from, name: mockConfig.senderName },
to: [recipient1, recipient2],
cc: undefined,
bcc: undefined,
Expand Down
2 changes: 1 addition & 1 deletion providers/mailersend/src/lib/mailersend.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class MailersendEmailProvider implements IEmailProvider {

const emailParams = new EmailParams()
.setFrom(options.from ?? this.config.from)
.setFromName(this.config.senderName)
.setFromName(options.senderName || this.config.senderName || '')
.setRecipients(recipients)
.setSubject(options.subject)
.setHtml(options.html)
Expand Down
2 changes: 1 addition & 1 deletion providers/mailjet/src/lib/mailjet.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class MailjetEmailProvider implements IEmailProvider {
const message: Mailjet.SendEmailV3_1.Message = {
From: {
Email: options.from || this.config.from,
Name: this.config.senderName,
Name: options.senderName || this.config.senderName,
},
To: options.to.map((email) => ({
Email: email,
Expand Down
4 changes: 2 additions & 2 deletions providers/mandrill/src/lib/mandrill.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export class MandrillProvider implements IEmailProvider {
): Promise<ISendMessageSuccessResponse> {
const mandrillSendOption = {
message: {
from_email: this.config.from,
from_name: this.config.senderName,
from_email: emailOptions.from || this.config.from,
from_name: emailOptions.senderName || this.config.senderName,
subject: emailOptions.subject,
html: emailOptions.html,
to: this.mapTo(emailOptions),
Expand Down
1 change: 1 addition & 0 deletions providers/netcore/src/lib/netcore.provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ jest.mock('axios');
const mockConfig = {
apiKey: 'test-key',
from: 'netcore',
senderName: "Novu's Team",
};

const mockEmailOptions: IEmailOptions = {
Expand Down
6 changes: 5 additions & 1 deletion providers/netcore/src/lib/netcore.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class NetCoreProvider implements IEmailProvider {
private config: {
apiKey: string;
from: string;
senderName: string;
}
) {
this.axiosInstance = axios.create({
Expand All @@ -43,7 +44,10 @@ export class NetCoreProvider implements IEmailProvider {
options: IEmailOptions
): Promise<ISendMessageSuccessResponse> {
const data: IEmailBody = {
from: { email: options.from || this.config.from },
from: {
email: options.from || this.config.from,
name: options.senderName || this.config.senderName,
},
subject: options.subject,
content: [
{
Expand Down
2 changes: 1 addition & 1 deletion providers/nodemailer/src/lib/nodemailer.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class NodemailerProvider implements IEmailProvider {
const sendMailOptions: SendMailOptions = {
from: {
address: options.from || this.config.from,
name: this.config.senderName || '',
name: options.senderName || this.config.senderName || '',
},
to: options.to,
subject: options.subject,
Expand Down
5 changes: 4 additions & 1 deletion providers/outlook365/src/lib/outlook365.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ export class Outlook365Provider implements IEmailProvider {

private createMailData(options: IEmailOptions): SendMailOptions {
const sendMailOptions: SendMailOptions = {
from: this.config.from,
from: {
address: options.from || this.config.from,
name: options.senderName || this.config.senderName,
},
to: options.to,
subject: options.subject,
html: options.html,
Expand Down
1 change: 1 addition & 0 deletions providers/plunk/src/lib/plunk.provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PlunkEmailProvider } from './plunk.provider';

const mockConfig = {
apiKey: 'sample-api-key',
senderName: "Novu's Team",
};

const mockNovuMessage = {
Expand Down
2 changes: 2 additions & 0 deletions providers/plunk/src/lib/plunk.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class PlunkEmailProvider implements IEmailProvider {
constructor(
private config: {
apiKey: string;
senderName: string;
}
) {
this.plunk = new Plunk(this.config.apiKey);
Expand Down Expand Up @@ -53,6 +54,7 @@ export class PlunkEmailProvider implements IEmailProvider {
): Promise<ISendMessageSuccessResponse> {
const response: IPlunkResponse = await this.plunk.emails.send({
from: options.from,
name: options.senderName || this.config.senderName,
to: options.to,
subject: options.subject,
body: options.html || options.text,
Expand Down
2 changes: 1 addition & 1 deletion providers/sendgrid/src/lib/sendgrid.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class SendgridEmailProvider implements IEmailProvider {
const mailData: Partial<MailDataRequired> = {
from: {
email: options.from || this.config.from,
name: this.config.senderName,
name: options.senderName || this.config.senderName,
},
...this.getIpPoolObject(options),
to: options.to.map((email) => ({ email })),
Expand Down
4 changes: 2 additions & 2 deletions providers/sendinblue/src/lib/brevo.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class BrevoEmailProvider implements IEmailProvider {
id = 'sendinblue';
channelType = ChannelTypeEnum.EMAIL as ChannelTypeEnum.EMAIL;
private axiosInstance: AxiosInstance;
public readonly BASE_URL = ' https://api.brevo.com/v3';
public readonly BASE_URL = 'https://api.brevo.com/v3';

constructor(
private config: {
Expand All @@ -35,7 +35,7 @@ export class BrevoEmailProvider implements IEmailProvider {
const email: any = {};
email.sender = {
email: options.from || this.config.from,
name: this.config.senderName,
name: options.senderName || this.config.senderName,
};
email.to = getFormattedTo(options.to);
email.subject = options.subject;
Expand Down
6 changes: 5 additions & 1 deletion providers/ses/src/lib/ses.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class SESEmailProvider implements IEmailProvider {
text,
to,
from,
senderName,
subject,
attachments,
cc,
Expand All @@ -50,7 +51,7 @@ export class SESEmailProvider implements IEmailProvider {
attachments,
from: {
address: from,
name: this.config.senderName,
name: senderName,
},
cc,
bcc,
Expand All @@ -68,9 +69,11 @@ export class SESEmailProvider implements IEmailProvider {
cc,
bcc,
replyTo,
senderName,
}: IEmailOptions): Promise<ISendMessageSuccessResponse> {
const info = await this.sendMail({
from: from || this.config.from,
senderName: senderName || this.config.senderName,
to: to,
subject: subject,
html: html,
Expand Down Expand Up @@ -162,6 +165,7 @@ export class SESEmailProvider implements IEmailProvider {
bcc: [],
cc: [],
replyTo: '[email protected]',
senderName: 'Novu Support',
});

return {
Expand Down

1 comment on commit 63f7fe8

@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.