Search code examples
node.jssmtpgmailsendmailcloud-foundry

Send Gmail SMTP email on Cloud Foundry Node.js?


I have this code below which works local but when I deploy to api.cloudfoundry.com, there is no email sent at all. Can you help me to understand why? Is it because Cloud Foundry prevent some port?

var port = (process.env.VMC_APP_PORT || 3000),
    host = (process.env.VCAP_APP_HOST || 'localhost'),
    http = require('http'),
    async = require('async');
var email   = require("emailjs");
var server  = email.server.connect({
   user:    "username", 
   password:"password", 
   host:    "smtp.gmail.com", 
   ssl:     true
});
// send the message and get a callback with an error or details of the message that was sent
http.createServer(function(req, res) {
async.series([
   function(callback){
server.send({
  text:    "This should be <b>BOLD</b>...", 
  from:    "XXX <[email protected]>", 
  to:      "YYY <[email protected]>, ZZZ <[email protected]>",
  cc:      "",
  subject: "emailjs: test HTML body 3",
   attachment: 
   [
      {data:"<h1>Big a$$ title</h1><br/>Or maybe this should be <b>BOLD</b> instead!!", alternative:true}

   ]
}, function(err, message) { console.log(err || message); });
       callback(null, 'success');
   }
],
function(err, results){
 res.writeHead(200, {'Content-Type': 'text/plain'});
 res.write(results + '...');
});
}).listen(port, host);

UPDATE 1:

Look like this is a gmail SMTP issue. I did vmc files myapp logs/stdout.log and it showed:

{ [Error: authorization.failed] code: 3, previous: { [Error: bad response on command 'cGludGVyZXN0'] code: 2, smtp: '535-5.7.1 Please log in with your web browser and then try again. Le arn more at\n535 5.7.1 https://support.google.com/mail/bin/answer.py?answer=7875 4 hn9sm22202393qab.8 - gsmtp\r\n' }, smtp: undefined }

I tested the other SMTP or different gmail account and it works. So it is specific to this gmail account I am testing.

I then followed this link https://support.google.com/mail/bin/answer.py?hl=en&answer=78754 to authorize the app

Next step

Sign in using the application you want to authorize access to your account within the next ten minutes. Google will remember the application after it signs in, and will allow it to access your account in the future as long as it uses the correct password.

And try to login again but still no luck. I thought I supposed to receive an email within gmail to approve, but nothing so far :(

UPDATE 2:

I tried with another gmail account and went through the step to approve Cloud Foundry for app access and it works. Apparently, for the account that didn't work, I missed the "10min window" to sign in and approve Cloud Foundry. Gmail never came back and asked to have it approved again. Miss is miss :(


Solution

  • I've just tried deploying this to Cloud Foundry myself, and it seems to work - although it probably is not good practice for your app not to return anything to the HTTP client (try res.end instead of res.write in your last function).

    You also should use VCAP_APP_PORT instead of VMC_APP_PORT, although both will work for now.

    In my case I also created a package.json file containing the async and emailjs dependencies, and ran npm install and npm shrinkwrap before testing locally and pushing the app to Cloud Foundry.

    What error are you seeing? did you check the logs on the server side (vmc logs command)?