2013-02-15 312 views
2

我有下面这个代码在本地工作,但当我部署到api.cloudfoundry.com,根本没有电子邮件发送。你能帮我理解为什么吗?是否因为Cloud Foundry阻止某个端口?在Cloud Foundry Node.js上发送Gmail SMTP电子邮件?

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); 

更新1:

的样子,这是一个Gmail的SMTP问题。我做vmc files myapp logs/stdout.log而事实证明:

{ [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 }

我测试的其他SMTP或不同的Gmail帐户和它的作品。所以这是特定于我正在测试的这个Gmail帐户。

我再接着这个链接https://support.google.com/mail/bin/answer.py?hl=en&answer=78754授权应用

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.

,并再次尝试,但仍然登录没有运气。我想我应该到Gmail批准之内收到一封电子邮件,但没有到目前为止:(

更新2:

我试着用另一个Gmail帐户,并通过台阶走到批准的Cloud Foundry的应用程序访问显然,对于没有工作的帐户,我错过了“10分钟窗口”来登录并批准Cloud Foundry.Gmail从未回来并要求再次批准。小姐错过了:(

回答

2

我刚刚尝试将其部署到Cloud Foundry本身,并且它似乎可行 - 尽管您的应用可能不会将任何内容返回给HTTP客户端t(尝试res.end而不是res.write在你的最后一个函数中)。

您也应该使用VCAP_APP_PORT而不是VMC_APP_PORT,尽管两者现在都可以使用。

在我的情况下,我还创建了一个包含async和emailjs依赖项的package.json文件,并在本地测试并将应用推送到Cloud Foundry之前运行npm install和npm shrinkwrap。

你看到了什么错误?你检查了服务器端的日志(vmc logs命令)吗?

+0

实际上,您可以将主机和端口保留为localhost:3000,Cloud Foundry将在生产中覆盖它。 – 2013-02-15 10:47:07

+1

我编辑了更多的细节...谢谢你的日志主意先生。 Cloud Foundry并不是这里的错。 – 2013-02-17 00:07:15