发送电子邮件nodemailer
问题描述:
从我的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
我看到拒绝的连接,但不明白为什么我得到这个错误。你认为缺失的部分是什么?
答
我觉得你的问题是这样的:
的命令行程序邮件使用一种称为/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.
找你吧,我拿到了第二条消息“的telnet:无法连接到远程主机:连接被拒绝”。实际上,我通过sendmail使用的是本地主机上运行的SMTP服务器。 – Luc
我安装了postfix,经过一点努力,它工作正常:)。非常感谢。 – Luc