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

标签 node.js sendmail nodemailer

从我的 ubuntu (10.04) 盒子,我可以毫无问题地发送电子邮件:

echo "hello" | mail -s 'test email' my_gmail_nickname@gmail.com

当我尝试从同一台机器上运行的 node.js 应用程序发送电子邮件时,它不起作用。

var nodemailer = require('nodemailer');
nodemailer.SMTP = {
  host: 'localhost'
}
nodemailer.send_mail(
{
    sender: 'me@example.com',
    to:'my_gmail_nickname@gmail.com',
    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

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

最佳答案

我觉得你的问题是这样的:

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

Node 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 有问题,它没有启动/启用监听端口 25 - 这是默认设置(出于安全原因)。您需要先配置它。

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

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

关于node.js - 使用 nodemailer 从 node.js 应用程序发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7105045/

相关文章:

node.js - 如何在 Angular 2 组件中使用 require ('os' ) 来获取 IP 地址?

node.js - 如何在 React Native 中导入 Node 模块

c# - 邮件不以表格格式发送 C#

email - 通过 EMAIL 在 Jenkins 中触发构建

node.js - 如何使用nodemailer发送html文件

javascript - 如何通过nestjs在同一 Controller 中使用多个diskStorage目的地

node.js - Nodemailer中XOAUTH2的优点

javascript - nodemailer 电子邮件登录无效

node.js - Node and sequelize -> .catch(...) 没有按预期工作