<commit_before>var nodemailer = require('nodemailer'),
    smtpTransport = require('nodemailer-smtp-transport');

var path           = require('path');
var emailTemplates = require('email-templates');
var fs = require('fs');

var logger = require(__dirname+'/../logger/logger');

var MAIL_CONFIG_FILE_PATH = process.cwd() + '/config/email.json',
    mailOptions;
    
var complexMailer = require(__dirname+'/complexMailer');

module.exports = {
    complex: complexMailer,
    send: function(to, subject, html, text, callback) {

        if (!transporter) {
            // Check if config file for database exists
            try {
                mailOptions = JSON.parse(fs.readFileSync(MAIL_CONFIG_FILE_PATH));
            }
            catch (err) {
                logger.error(new Error(MAIL_CONFIG_FILE_PATH + ' doesn\'t exists or it\'s not a valid JSON.'), 'Mailer');
                process.exit(1);
            }

            if (!mailOptions || !mailOptions.port || !mailOptions.host || !mailOptions.from) {
                logger.error(new Error('mail config file is invalid.'), 'Mailer');
                process.exit(1);
            }

            var transport = {
                port: mailOptions.port,
                host: mailOptions.host,
                maxConnections: 5,
                maxMessages: 10
            };

            if (mailOptions.auth) {
                transport.auth = {
                    user: mailOptions.auth.user,
                    pass: mailOptions.auth.pass
                };
                transport.secure = true;
            }

            var transporter = nodemailer.createTransport(smtpTransport(transport));
        }

        var optionsForTransporter = {
            from: mailOptions.from, // sender address
            to: to, // list of receivers
            subject: subject,
            html: html,
            text: text
        };

        //if (process.env.NODE_ENV != 'development') {
        transporter.sendMail(optionsForTransporter, function (err, info) {
            if (callback)
                return callback(err, info);
        });
        //}
    },
    mail: function(mail, templateName, module, lang, params, callback) {
        var $this = this;
        var pathi18n;
        var pathMailModule;

        pathMailModule = process.cwd() + '/src/modules/' + module + '/email';
        pathi18n = pathMailModule + '/i18n/' + lang;

        params.i18n = require(pathi18n);
        emailTemplates(pathMailModule, function (err, template) {
            if (err) {
                if (callback)
                    return callback(err);
                throw err;
            }
            template(templateName, params, function (err, html, text) {
                if (err) {
                    if (callback)
                        return callback(err);
                    throw err;
                }
                $this.send(mail, params.i18n[templateName].title, html, text.replace(/&#39;/g, "'"), function (err, info) {
                    if (callback)
                        return callback(err, info);
                });
            });
        });
    }
};<commit_msg>attachments ;)<commit_after>var nodemailer = require('nodemailer'),
    smtpTransport = require('nodemailer-smtp-transport');

var path           = require('path');
var emailTemplates = require('email-templates');
var fs = require('fs');

var logger = require(__dirname+'/../logger/logger');

var MAIL_CONFIG_FILE_PATH = process.cwd() + '/config/email.json',
    mailOptions;

module.exports = {
    send: function(to, subject, html, text, attachments, callback) {

        if (typeof attachments == 'function') {
            callback = attachments;
            attachments = null;
        }

        if (!transporter) {
            // Check if config file for database exists
            try {
                mailOptions = JSON.parse(fs.readFileSync(MAIL_CONFIG_FILE_PATH));
            }
            catch (err) {
                logger.error(new Error(MAIL_CONFIG_FILE_PATH + ' doesn\'t exists or it\'s not a valid JSON.'), 'Mailer');
                process.exit(1);
            }

            if (!mailOptions || !mailOptions.port || !mailOptions.host || !mailOptions.from) {
                logger.error(new Error('mail config file is invalid.'), 'Mailer');
                process.exit(1);
            }

            var transport = {
                port: mailOptions.port,
                host: mailOptions.host,
                maxConnections: 5,
                maxMessages: 10
            };

            if (mailOptions.auth) {
                transport.auth = {
                    user: mailOptions.auth.user,
                    pass: mailOptions.auth.pass
                };
                transport.secure = true;
            }

            var transporter = nodemailer.createTransport(smtpTransport(transport));
        }

        var optionsForTransporter = {
            from: mailOptions.from, // sender address
            to: to, // list of receivers
            subject: subject,
            html: html,
            text: text,
            attachments: attachments
        };

        //if (process.env.NODE_ENV != 'development') {
        transporter.sendMail(optionsForTransporter, function (err, info) {
            if (callback)
                return callback(err, info);
        });
        //}
    },
    mail: function(mail, templateName, module, lang, params, attachments, callback) {
        if (typeof attachments == 'function') {
            callback = attachments;
            attachments = null;
        }
        var $this = this;
        var pathi18n;
        var pathMailModule;

        pathMailModule = process.cwd() + '/src/modules/' + module + '/email';
        pathi18n = pathMailModule + '/i18n/' + lang;

        params.i18n = require(pathi18n);
        emailTemplates(pathMailModule, function (err, template) {
            if (err) {
                if (callback)
                    return callback(err);
                throw err;
            }
            template(templateName, params, function (err, html, text) {
                if (err) {
                    if (callback)
                        return callback(err);
                    throw err;
                }

                $this.send(mail, params.i18n[templateName].title, html, text.replace(/&#39;/g, "'"), attachments, function (err, info) {
                    if (callback)
                        return callback(err, info);
                });
            });
        });
    }
};
{'fraction_non_alphanumeric': 0.11518563603164943, 'fraction_numerical': 0.005782105903834449, 'mean_word_length': 1.5329479768786127, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '725533', 'n_tokens_mistral': 1770, 'n_tokens_neox': 1660, 'n_words': 391}