

var SendGrid = require('sendgrid').SendGrid
    ,Email = require('sendgrid').Email;

var sendgrid, from;

exports.load = function(app)
{
    sendgrid = new SendGrid(app.settings.sendgrid_user, app.settings.sendgrid_key);
    from = app.settings.system_email;
};

var sendMail = exports.sendMail = function(to,body,subject,callback) {


    if(!sendgrid) {
        console.log('email not sent because it\'s off in app.js. to turn mails on set app.set("send_mails",true); ');
        callback();
        return;
    }

    console.log('sending to ' + to + ' ' + subject);

    var email = new Email({
        to:[to],
        from:from,
        fromname:'אתר "דמוקראסי" לעריכת נייר עמדה בתגובה למתווה הגז',
        subject:subject,
        html:body
    });

    console.log('mail sent to ' + to);

    sendgrid.send(email,function(success,message) {
        if(!success){
            console.error('mail was not sent');
            console.error(message);
            callback(message);
        }
        else{
            console.log('mail was sent');
            callback(null,message);
        }
    });

    //sendSMTPMail(to, from,  body, subject, function(err) {
    //        if(err){
    //            console.error('mail was not sent');
    //            callback('mail was not sent');
    //        }
    //        else{
    //            console.log('mail was sent');
    //            callback(null, 'mail was sent');
    //        }
    //    });

};

var sendMailFromTemplate = exports.sendMailFromTemplate = function(to, string,callback) {
    var parts = string.split('<!--body -->');
    var subject = parts[0] || 'אתר "דמוקראסי" לעריכת נייר עמדה בתגובה למתווה הגז';
    var body = parts[1] || parts[0];
    sendMail(to,body,subject,callback);
};

var sendSMTPMail = module.exports.sendSMTPMail = function (userMail, from, body, subject, callback) {
    var nodemailer = require('nodemailer');
    var smtpTransport = require('nodemailer-smtp-transport');

    // create reusable transporter object using SMTP transport
    var transporter = nodemailer.createTransport(smtpTransport({
        secure: false,
        debug: true,
        tls: {rejectUnauthorized: false}
    }));

    var mailOptions = {
        from: from , // sender address
        to: userMail, // list of receivers
        subject: subject, // Subject line
        html: body // html body
    };
    //
    //// send mail with defined transport object
    transporter.sendMail(mailOptions, function(error, info){
        if(error){
            console.log(error);
            callback(null, info);
        }else{
            console.log('Message sent: ' + info.response);
            callback();
        }
    });
};

@@ -647,35 +647,24 @@
 romname:'%D7%90%D7%AA%D7%A8
- %22%D7%93%D7%9E%D7%95%D7%A7%D7%A8%D7%90%D7%A1%D7%99%22
  %D7%9C%D7%A2%D7%A8%D7%99%D7%9B%D7%AA %D7%A0%D7%99%D7%99%D7%A8
@@ -1622,19 +1622,8 @@
 '%D7%90%D7%AA%D7%A8
- %22%D7%93%D7%9E%D7%95%D7%A7%D7%A8%D7%90%D7%A1%D7%99%22
  %D7%9C%D7%A2%D7%A8

Update mail.js