UNPKG

@softkit/mail

Version:

The Mailgun Mail Module is a comprehensive solution for integrating Mailgun's email functionality into NestJS applications. It provides a seamless way to send emails using Mailgun with minimal setup and configuration.

75 lines 3.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MailgunService = void 0; const tslib_1 = require("tslib"); const common_1 = require("@nestjs/common"); const abstract_mail_service_1 = require("../abstract-mail.service"); const constants_1 = require("../../constants"); const config_1 = require("../../config"); const exceptions_1 = require("@softkit/exceptions"); let MailgunService = class MailgunService extends abstract_mail_service_1.AbstractMailService { constructor(mailgun, config) { super(); this.mailgun = mailgun; this.config = config; } async sendEmail(emailData) { const fromAddress = emailData.from ?? this.config.defaultFromEmail; const bcc = emailData.bcc ?? this.config.defaultBccList; const { attachment, cc, html, subject, text, ...rest } = emailData; if (html || text) { const content = (html ? { html } : { text }); return this.mailgun.messages.create(this.config.domain, { from: fromAddress, bcc, name: emailData.userFullName, attachment, cc, subject, ...content, ...rest, }); } else { throw new exceptions_1.GeneralInternalServerException(undefined, `Looks like a developer mistake either html or text must be provided. Take a look now.`); } } async sendTemplateEmail(templateId, emailData, emailTemplateParams) { const templateVariables = {}; if (emailTemplateParams) { for (const [key, value] of Object.entries(emailTemplateParams)) { if (typeof value === 'object' || Array.isArray(value)) { templateVariables[`v:${key}`] = JSON.stringify(value); } else { templateVariables[`v:${key}`] = String(value); } } } else { this.logger.log(`sendTemplateEmail: 'emailTemplateParams' is undefined or null. The email will be sent without template variables.`); } const fromAddress = emailData.from ?? this.config.defaultFromEmail; const bcc = emailData.bcc ?? this.config.defaultBccList; const { attachment, cc, subject, ...rest } = emailData; return this.mailgun.messages.create(this.config.domain, { from: fromAddress, name: emailData.userFullName, attachment, cc, bcc, subject, template: templateId, ...rest, ...templateVariables, }); } }; exports.MailgunService = MailgunService; exports.MailgunService = MailgunService = tslib_1.__decorate([ (0, common_1.Injectable)(), tslib_1.__param(0, (0, common_1.Inject)(constants_1.MAILGUN_CLIENT_TOKEN)), tslib_1.__param(1, (0, common_1.Inject)(constants_1.MAILGUN_CONFIG_TOKEN)), tslib_1.__metadata("design:paramtypes", [Object, config_1.MailgunConfig]) ], MailgunService); //# sourceMappingURL=mailgun.service.js.map