var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
var transport = nodemailer.createTransport(smtpTransport({
host: 'mail.mydomain.com',
port: 587,
secure: true,
auth: {
user: 'hello',
pass: 'thepassword'
}
}));
var mailOptions = {
from: 'Tester <test@mydomain.com>', // sender address
to: 'someemail@gmail.com', // list of receivers
subject: 'Hello', // Subject line
text: 'hey', // plaintext body
html: 'hey' // html body
};
transport.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log('Message sent: ' + info.response);
}
});
我正在尝试连接到我自己的postfix服务器以通过SMTP发送电子邮件。但是,当我运行它时,我得到:
[Error: 139969407567744:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:../deps/openssl/openssl/ssl/s23_clnt.c:787: ]
我确定我的SMTP端口是587.我确定我的Postfix服务器上启用了SSL。我通过Mac上的Mail应用程序连接SSL。
这是我的后缀配置:
myhostname = mail.mydomain.com
myorigin = mail.mydomain.com
mydestination = mail.mydomain.com, mydomain.com, localhost, localhost.localdomain
relayhost =
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
alias_maps = hash:/etc/aliases, regexp:/etc/aliases_regex
#alias_database = hash:/etc/aliases
smtpd_tls_CAfile=/etc/postfix/tls/startssl-ca-bundle.pem
smtpd_tls_cert_file=/etc/postfix/tls/mail.mydomain.crt
smtpd_tls_key_file=/etc/postfix/tls/mail.mydomain.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtpd_tls_security_level=may
smtpd_tls_protocols = !SSLv2, !SSLv3
#local_recipient_maps = proxy:unix:passwd.byname $alias_maps
milter_default_action = accept
milter_protocol = 2
smtpd_milters = inet:127.0.0.1:8891
non_smtpd_milters = inet:127.0.0.1:8891
virtual_alias_domains =mydomain.com
virtual_alias_maps = hash:/etc/postfix/virtual
我正在使用StartSSL作为我的Postfix服务器。在我的Node.js服务器上,我看到了:
StartCom_Certification_Authority_2.pem
StartCom_Certification_Authority_G2.pem
StartCom_Certification_Authority.pem
/etc/ssl
中的。所以,我假设我的Node.js服务器已经安装了根证书。
当我运行上面的Node.js代码时,我会在mail.log
上找到它:
Sep 13 04:08:30 d-mail postfix/submission/smtpd[724]: connect from unknown[107.170.206.11]
Sep 13 04:08:30 d-mail postfix/submission/smtpd[724]: lost connection after UNKNOWN from unknown[107.170.206.11]
Sep 13 04:08:30 d-mail postfix/submission/smtpd[724]: disconnect from unknown[107.170.206.11]
我不知道为什么我无法从我的Node.js应用程序连接,但我可以在我自己的Macbook上完全连接它。
我是否需要在Node.js服务器上安装一些证书?
答案 0 :(得分:2)
使用端口587,您不能直接使用SSL,而是以纯文本开头,然后使用STARTTLS命令升级到SSL。这意味着secure
必须为false。请参阅https://github.com/andris9/nodemailer-smtp-transport。