techxnix-mail
Version:
Techxnix Mail Library for NestJS applications - A powerful and easy-to-use mail module using SendGrid
100 lines • 4.89 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var MailService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MailService = void 0;
const common_1 = require("@nestjs/common");
const SendGrid = require("@sendgrid/mail");
const metadata_constants_1 = require("../constants/metadata.constants");
let MailService = MailService_1 = class MailService {
constructor(config) {
this.config = config;
this.logger = new common_1.Logger(MailService_1.name);
SendGrid.setApiKey(this.config.sendGridApiKey);
this.logger.log('SendGrid API Key configured');
}
async send(options) {
var _a, _b, _c, _d, _e, _f;
try {
this.logger.log(`Attempting to send email to: ${options.to}`);
this.logger.debug('Email options:', {
to: options.to,
subject: options.subject,
templateId: options.templateId,
hasHtml: !!options.html,
hasText: !!options.text,
hasAttachments: !!((_a = options.attachments) === null || _a === void 0 ? void 0 : _a.length),
});
const defaultFromEmail = this.config.defaultFromEmail || 'noreply@progpt.com';
const defaultFromName = this.config.defaultFromName || 'ProGPT';
const msg = {
to: options.to,
subject: options.subject,
from: {
email: (_c = (_b = options.from) === null || _b === void 0 ? void 0 : _b.email) !== null && _c !== void 0 ? _c : defaultFromEmail,
name: (_e = (_d = options.from) === null || _d === void 0 ? void 0 : _d.name) !== null && _e !== void 0 ? _e : defaultFromName,
},
text: options.text,
html: options.html,
templateId: options.templateId,
dynamicTemplateData: options.dynamicTemplateData,
attachments: options.attachments,
};
const [response] = await SendGrid.send(msg);
if ((response === null || response === void 0 ? void 0 : response.statusCode) >= 200 && (response === null || response === void 0 ? void 0 : response.statusCode) < 300) {
this.logger.log(`Email sent successfully to ${options.to}. Status: ${response.statusCode}`, {
statusCode: response.statusCode,
headers: response.headers,
});
return true;
}
else {
this.logger.warn(`Unexpected status code when sending email to ${options.to}`, {
statusCode: response === null || response === void 0 ? void 0 : response.statusCode,
headers: response === null || response === void 0 ? void 0 : response.headers,
});
return false;
}
}
catch (error) {
this.logger.error(`Failed to send email to ${options.to}: ${error.message}`, {
stack: error.stack,
response: (_f = error.response) === null || _f === void 0 ? void 0 : _f.body,
code: error.code,
});
return false;
}
}
async sendTemplate(to, templateId, dynamicTemplateData, options = {}) {
this.logger.log(`Attempting to send template email to: ${to}`);
this.logger.debug('Template details:', {
templateId,
dynamicTemplateData,
});
return this.send({
to,
subject: options.subject || '',
templateId,
dynamicTemplateData,
from: options.from,
});
}
};
exports.MailService = MailService;
exports.MailService = MailService = MailService_1 = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)(metadata_constants_1.MAIL_CONFIG)),
__metadata("design:paramtypes", [Object])
], MailService);
//# sourceMappingURL=mail.service.js.map