From 3e2e560d04c37fb7a825ada44809e53f2ccd28af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nuno=20Carvalh=C3=A3o?= Date: Sun, 11 Apr 2021 19:58:45 +0100 Subject: [PATCH] feat(classes): allow template variables --- package-lock.json | 2 +- package.json | 2 +- .../classes/mailgun-email-model.ts | 20 +++++++++++++++++++ .../services/relay/mailgun.service.ts | 7 +++---- 4 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 src/nestjs-mailgun/classes/mailgun-email-model.ts diff --git a/package-lock.json b/package-lock.json index 4922653..fca6cde 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@nextnm/nestjs-mailgun", - "version": "1.0.4", + "version": "1.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 3512499..6c8a0f1 100644 --- a/package.json +++ b/package.json @@ -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 ", "license": "MIT", diff --git a/src/nestjs-mailgun/classes/mailgun-email-model.ts b/src/nestjs-mailgun/classes/mailgun-email-model.ts new file mode 100644 index 0000000..c79638a --- /dev/null +++ b/src/nestjs-mailgun/classes/mailgun-email-model.ts @@ -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']; + } +} diff --git a/src/nestjs-mailgun/services/relay/mailgun.service.ts b/src/nestjs-mailgun/services/relay/mailgun.service.ts index 1ab637e..ef2003d 100644 --- a/src/nestjs-mailgun/services/relay/mailgun.service.ts +++ b/src/nestjs-mailgun/services/relay/mailgun.service.ts @@ -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; @@ -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 { @@ -30,7 +29,7 @@ export class MailgunService { }); } - public sendEmail(emailOptions: EmailOptions): Promise { + public sendEmail(emailOptions: EmailOptions | MailgunEmailModel): Promise { return new Promise((resolve, reject) => { this.mailgun.messages().send(emailOptions, (error, body) => { if (error) {