Wednesday, 12 April 2017

Nodemailer not sending response

I'm working on my first express project. I'm using Nodemailer and it's sending emails just fine but I'm not getting a response. Any insights? Here is the function I'm using to send the emails. It console logs message sent just fine, but doesn't seem to respond.

exports.email = function(req, res, options, tokens) {
    receiver_id= options.email.receiver_id;
    message= options.email.message;
    sender_email = options.email.sender_email;
    sender_name = options.email.sender_name;
    slug=options.info.slug;
    org_name=options.info.org_name;
    dir_url=options.info.dir_url;    

    var https = require('https');
    str = '';
    //getting user email by id    

    path = '/api/v1/people/' + receiver_id +'?__proto__=&access_token=' + tokens[slug];
    var options = {
         host: slug + '.nationbuilder.com',
         path: path,
         method: "GET",
         json: true,
         headers: {
             "content-type": "application/json",
             "accept": "application/json"
         },
    }    

    var nb_req = https.get(options, req_callback);    

    function req_callback(response, res) {    

        response.on('data', function(chunk) {
            str += chunk;
        });    

        response.on('end', function() {
            object = JSON.parse(str);
            receiver_email = object.person.email1;
            var nodemailer = require('nodemailer');    

            var transporter = nodemailer.createTransport({
                    port:465,
                    host:"smtp.gmail.com",
                    auth: {
                        user: 'connect@alumninations.com', // Your email id
                        pass: '69$6FK$b6PQu' // Your password
                    }
                });    

                var html = '<p>From: ' + sender_email + '<br>';
                html += 'To: ' + receiver_email + '</p>';
                html += "<p>"+message + "</p>"
                html += "<p>This message was delivered by the " + org_name + " alumni directory website, " + "<a href='" + dir_url
                + "'>" + dir_url + "</p>";



                var mailOptions = {
                    from: 'connect@alumninations.com', // sender address
                    to: 'petervankoughnett@gmail.com', // list of receivers
                    subject: "A message from " + sender_name, // Subject line
                    /*text: "Message sent to", // plaintext body*/
                    html: html 
                };    

                transporter.sendMail(mailOptions, function(error, info){
                    if(error){
                        console.log(error);
                        res.json({yo: 'error'});
                    }else{
                        console.log('Message sent: ' + info.response);
                        res.sendStatus(200);
                    };
                    return res.sendStatus(200);    

                });    


        });    

    }    

}



via Peter Breen

No comments:

Post a Comment