UNPKG

@aarconada/urserver

Version:

Basic Server definitions to develope REST API with a node + express Server

84 lines (80 loc) 2.77 kB
'use strict'; const server = require('./server')(); const path = require('path'); const nodemailer = require('nodemailer'); var transporter = null; try { server.debug('Initializing email service...'); transporter = nodemailer.createTransport(server.configuration.email.configuration); server.debug('Email service initialized') } catch(ex){ //server.debug('Unable initialize email service'); } var sendTextEmail = function(to, subject, text) { if(transporter) { const mailOptions = { from: server.configuration.email.sender, to: to, subject: subject, text: text }; server.debug('Sending text email to ', to); return transporter.sendMail(mailOptions, function (err, info) { if (err) { server.debug(err); throw server.defaultResponses.email_unable_send; } server.debug('Email sended: ', info); }); } else { throw server.defaultResponses.email_not_configured; } }; var sendHtmlEmail = function(to, subject, html) { if (transporter) { const mailOptions = { from: server.configuration.email.sender, to: to, subject: subject, html: html }; server.debug('Sending html email to ', to); return transporter .sendMail(mailOptions) .then(info => { server.debug('Email sended: ', info); }) .catch(err => { server.debug(err); throw server.defaultResponses.email_unable_send; }); } else { throw server.defaultResponses.email_not_configured; } }; var sendHtmlEmailWithAttachments = function(to, subject, html, attachments) { if (transporter) { const mailOptions = { from: server.configuration.email.sender, to: to, subject: subject, html: html, attachments: attachments }; server.debug('Sending html email to ', to); return transporter .sendMail(mailOptions) .then(info => { server.debug('Email sended: ', info); }) .catch(err => { server.debug(err); throw server.defaultResponses.email_unable_send; }); } else { throw server.defaultResponses.email_not_configured; } }; module.exports.sendTextEmail = sendTextEmail; module.exports.sendHtmlEmail = sendHtmlEmail; module.exports.sendHtmlEmailWithAttachments = sendHtmlEmailWithAttachments;