2011-08-18 185 views
2

從我的Ubuntu(10.04)中,我沒有問題發送電子郵件:發送電子郵件nodemailer

echo "hello" | mail -s 'test email' [email protected] 

當我嘗試從node.js的應用程序運行在發送電子郵件同一臺機器,它不起作用。

var nodemailer = require('nodemailer'); 
nodemailer.SMTP = { 
    host: 'localhost' 
} 
nodemailer.send_mail(
{ 
    sender: '[email protected]', 
    to:'[email protected]', 
    subject:'Hello!', 
    html: 'test', 
    body:'test' 
}, 
function(error, success){ 
    console.log(error); 
    console.log(success); 
    console.log('Message ' + success ? 'sent' : 'failed'); 
}); 

我有錯誤消息:

[email protected]:~/gridteams/services/gpshop$ cat nohup.out 
{ stack: [Getter/Setter], 
    arguments: undefined, 
    type: undefined, 
    message: 'ECONNREFUSED, Connection refused', 
    errno: 111, 
    code: 'ECONNREFUSED', 
    syscall: 'connect' } 
null 
sent 

我看到拒絕的連接,但不明白爲什麼我得到這個錯誤。你認爲缺失的部分是什麼?

回答

7

我覺得你的問題是這樣的:

的命令行程序郵件使用一種稱爲/usr/sbin目錄/ sendmail的發送郵件的二進制文件。 sendmail是一個命令行程序,它將嘗試發送郵件。它使用本地連接到郵件基礎結構。

nodemailer會嘗試連接到SMTP服務器的TCP端口25上的主機本地主機不存在的節點。只要嘗試使用telnet程序進行連接以進行驗證即可。

這裏是一個服務器上運行:

$ telnet localhost 25 
Trying 127.0.0.1... 
Connected to localhost. 
Escape character is '^]'. 
220 xxxx.de ESMTP Postfix 
QUIT 
221 2.0.0 Bye 
Connection closed by foreign host. 

這裏沒有服務器上運行:

$ telnet localhost 25 
Trying 127.0.0.1... 
telnet: Unable to connect to remote host: Connection refused 

如果你得到第二個,你有你的SMTP一個問題,沒有啓動/啓用監聽在端口25上 - 這是默認的(出於安全原因)。您需要先配置它。

或者 - 根據nodemail文檔,您可以使用sendmail程序,以及:

'sendmail' alternative 

Alternatively if you don't want to use SMTP but the sendmail 

命令然後設置屬性的sendmail爲true(或路徑sendmail的 如果命令是不默認路徑)。

nodemailer.sendmail = true; 

or 

nodemailer.sendmail = '/path/to/sendmail'; 

If sendmail is set, then SMTP options are discarded. 
+0

找你吧,我拿到了第二條消息「的telnet:無法連接到遠程主機:連接被拒絕」。實際上,我通過sendmail使用的是本地主機上運行的SMTP服務器。 – Luc

+1

我安裝了postfix,經過一點努力,它工作正常:)。非常感謝。 – Luc