UNPKG

@sync-in/server

Version:

The secure, open-source platform for file storage, sharing, collaboration, and sync

93 lines (92 loc) 4 kB
/* * Copyright (C) 2012-2025 Johan Legrand <johan.legrand@sync-in.com> * This file is part of Sync-in | The open source file sync and share solution * See the LICENSE file for licensing details */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "Mailer", { enumerable: true, get: function() { return Mailer; } }); const _common = require("@nestjs/common"); const _config = require("@nestjs/config"); const _nestjspino = require("nestjs-pino"); const _nodemailer = /*#__PURE__*/ _interop_require_default(require("nodemailer")); function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _ts_decorate(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; } function _ts_metadata(k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); } let Mailer = class Mailer { async sendMails(mails) { if (!this.available) { return; } for (const m of mails){ this.transporter.sendMail(m).then(()=>{ this.logger.info(`Mail sent to '${m.to}' with subject '${m.subject}'`); }).catch((e)=>{ this.logger.error(`Mail was not sent to '${m.to}' with subject '${m.subject}' : ${e}`); }); } } async verify() { try { await this.transporter.verify(); this.logger.info(`Using Mail Server at ${this.configuration.host}:${this.configuration.port} (secure: ${this.configuration.secure})`); this.available = true; } catch (e) { this.logger.error(`Unable to use Mail Server at ${this.configuration.host}:${this.configuration.port} (secure: ${this.configuration.secure}) : ${e}`); this.available = false; } } constructor(configService, logger){ this.configService = configService; this.logger = logger; this.available = false; this.logger.setContext(Mailer.name.toUpperCase()); this.configuration = this.configService.get('mail'); if (this.configuration) { this.logger.logger.level = this.configuration.debug ? 'debug' : 'info'; if (this.configuration.secure && (this.configuration.port === 587 || this.configuration.port === 25)) { this.logger.warn(`Secure transport has been disabled due to use of port : ${this.configuration.port}`); this.configuration.secure = false; } this.transporter = _nodemailer.default.createTransport({ host: this.configuration.host, port: this.configuration.port, auth: this.configuration.auth, secure: this.configuration.secure, logger: this.configuration.logger ? this.logger : false }, { from: this.configuration.sender, tls: { rejectUnauthorized: false } }); this.verify().catch((e)=>this.logger.error(e)); } } }; Mailer = _ts_decorate([ (0, _common.Injectable)(), _ts_metadata("design:type", Function), _ts_metadata("design:paramtypes", [ typeof _config.ConfigService === "undefined" ? Object : _config.ConfigService, typeof _nestjspino.PinoLogger === "undefined" ? Object : _nestjspino.PinoLogger ]) ], Mailer); //# sourceMappingURL=mailer.service.js.map