Skip to content

Commit

Permalink
feat(classes): allow template variables
Browse files Browse the repository at this point in the history
  • Loading branch information
NextNC committed Apr 11, 2021
1 parent b4720fd commit 3e2e560
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nextnm/nestjs-mailgun",
"version": "1.0.4",
"version": "1.1.0",
"description": "NestJS provider for sending emails with mailgun",
"author": "Nuno Carvalhão <[email protected]>",
"license": "MIT",
Expand Down
20 changes: 20 additions & 0 deletions src/nestjs-mailgun/classes/mailgun-email-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { EmailOptions } from '../services/relay/mailgun.service';

export class MailgunEmailModel implements EmailOptions {
public 'h:X-Mailgun-Variables'?: string;

constructor(
public from: string,
public to: string | string[],
public subject: string,
public text?: string,
public html?: string,
public template?: string,
public attachment?: any,
public templateVariables?: { [key: string]: any },
) {
this['h:X-Mailgun-Variables'] = templateVariables
? JSON.stringify(templateVariables)
: this['h:X-Mailgun-Variables'];
}
}
7 changes: 3 additions & 4 deletions src/nestjs-mailgun/services/relay/mailgun.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable, Inject } from '@nestjs/common';
import { MAILGUN_CONFIGURATION } from '../../tokens/tokens';
import * as mailgunConfig from 'mailgun-js';
import { ConfigurationMailgun } from '../../../nestjs-mailgun/configuration';
import { MailgunEmailModel } from '../../../nestjs-mailgun/classes/mailgun-email-model';

export interface EmailOptions {
from: string;
Expand All @@ -11,9 +12,7 @@ export interface EmailOptions {
html?: string;
template?: string;
attachment?;
'recipient-variables'?: {
[email: string]: any;
};
'h:X-Mailgun-Variables'?: string;
}
@Injectable()
export class MailgunService {
Expand All @@ -30,7 +29,7 @@ export class MailgunService {
});
}

public sendEmail(emailOptions: EmailOptions): Promise<boolean> {
public sendEmail(emailOptions: EmailOptions | MailgunEmailModel): Promise<any> {
return new Promise((resolve, reject) => {
this.mailgun.messages().send(emailOptions, (error, body) => {
if (error) {
Expand Down

0 comments on commit 3e2e560

Please sign in to comment.