为什么'nodemailer'不发送任何消息?
Why 'nodemailer' does not send any message?
代码如下:
var nodemailder = require('nodemailer');
var from = nodemailder.createTransport({
service: "gmail",
auth:{
user: 'user***@gmail.com',
pass: 'pass***'
}
});
var options = {
from: 'user***@gmail.com',
to: 'user***@gmail.com',
subject: 'sometext',
text: 'messageContext',
};
from.sendMail(options, function (error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
如果我使用 Windows - 它有效,当我尝试从 Ubuntu 开始时 (16) - 我没有收到任何错误消息等,但是没有收到邮件。
如果是这样,我认为 SMTP 服务无法连接到端口 25。
运行 服务器看起来像:
$ 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
这意味着您的服务可能未 运行 或未启用。您需要先在端口 25 上 enable/run 此服务。
此外,根据 nodemail 文档,您也可以使用 sendmail 二进制文件:
'sendmail' alternative
Alternatively if you don't want to use SMTP but the sendmail
command 然后将 属性 sendmail 设置为 true(如果命令不在默认路径中,则作为 sendmail 的路径)。
nodemailer.sendmail = true;
or
nodemailer.sendmail = '/path/to/sendmail';
If sendmail is set, then SMTP options are discarded.
代码如下:
var nodemailder = require('nodemailer');
var from = nodemailder.createTransport({
service: "gmail",
auth:{
user: 'user***@gmail.com',
pass: 'pass***'
}
});
var options = {
from: 'user***@gmail.com',
to: 'user***@gmail.com',
subject: 'sometext',
text: 'messageContext',
};
from.sendMail(options, function (error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
如果我使用 Windows - 它有效,当我尝试从 Ubuntu 开始时 (16) - 我没有收到任何错误消息等,但是没有收到邮件。
如果是这样,我认为 SMTP 服务无法连接到端口 25。
运行 服务器看起来像:
$ 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
这意味着您的服务可能未 运行 或未启用。您需要先在端口 25 上 enable/run 此服务。
此外,根据 nodemail 文档,您也可以使用 sendmail 二进制文件:
'sendmail' alternative
Alternatively if you don't want to use SMTP but the sendmail
command 然后将 属性 sendmail 设置为 true(如果命令不在默认路径中,则作为 sendmail 的路径)。
nodemailer.sendmail = true;
or
nodemailer.sendmail = '/path/to/sendmail';
If sendmail is set, then SMTP options are discarded.