使用nodemailer从node.js应用程序发送电子邮件

发布于 2024-11-30 05:28:13 字数 1287 浏览 2 评论 0原文

从我的 ubuntu (10.04) 盒子中,我可以通过以下方式发送电子邮件:

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

我收到错误消息:

me@luc:~/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

我看到连接被拒绝,但不明白为什么会收到此错误。您认为缺失的部分是什么?

From my ubuntu (10.04) box, I have no problem send email with:

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

When I try to send an email from a node.js app running on the same machine, it does not work.

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

I have the error message:

me@luc:~/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

I see the connection refused but do not understand why I get this error. What do you think is the missing piece ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落□心尘 2024-12-07 05:28:13

我认为你的问题是这样的:

命令行程序 mail 使用名为 /usr/sbin/sendmail 的二进制文件来发送邮件。 sendmail 是一个命令行程序,它将尝试传递邮件。它使用到邮件基础设施的本地连接。

节点nodemailer将尝试连接到主机localhost上的TCP端口25上的SMTP服务器,但该服务器不存在。只需尝试使用 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 有问题,该 SMTP 未启动/启用在端口 25 上侦听 - 这是默认值(出于安全原因)。您需要先配置它。

或者 - 根据 nodemail 文档,您也可以使用 sendmail 二进制文件:

'sendmail' 替代方案

或者,如果您不想使用 SMTP 而使用 sendmail

命令然后将属性 sendmail 设置为 true (或作为 sendmail 的路径
如果命令不在默认路径中)。

nodemailer.sendmail = true;

或者

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

如果设置了 sendmail,则 SMTP 选项将被丢弃。

I think your problem is like this:

The commandline program mail uses a binary called /usr/sbin/sendmail for sending mail. sendmail is a commandline programm which will try to deliver the mail. It uses a local connection to the mail infrastructure.

The node nodemailer will try to connect to a SMTP server on the host localhost on TCP port 25 which doesn't exist. Just try to get a connection using the telnet program for verification.

Here is a server running:

$ 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.

here is no server running:

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

If you get the second, you have a problem with your SMTP which is not started/enabled listening on port 25 - which is the default (for security reasons). You need to configure it first.

Or - according to the nodemail documentation you can use the sendmail binary as well:

'sendmail' alternative

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

command then set property sendmail to true (or as the path to sendmail
if the command is not in default path).

nodemailer.sendmail = true;

or

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

If sendmail is set, then SMTP options are discarded.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文