2013-02-15 322 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