如何使用邮件服务器和nodejs从我的服务器发送电子邮件

时间:2013-06-19 17:51:14

标签: javascript node.js smtp postfix-mta nodemailer

我家里有一台带有静态IP的服务器,我用域名提供自己的网页,一切正常。

在我的网页中,您可以通过电子邮件和密码进行注册。当您注册一个名为nodemailer的节点模块时,从谷歌帐户发送一封电子邮件,问题是谷歌帐户对发送的电子邮件有限制。

所以我需要将nodemailer模块连接到我自己家中的服务器。

我在google上搜索,但没有类似的答案。

如何将postfix与nodejs一起使用?

http://www.postfix.org/

或如何将haraka模块与nodemailer一起使用

https://github.com/baudehlo/Haraka

所以我重复我的问题,我需要从我的服务器发送一封电子邮件给任何在我的网站上注册的电子邮件用户。

例如......

用户jose@gmail.com在我的网站上注册

nodemailer配置是......

exports.newRegistration=function(parametros,callback)
{
    var mailOptions = 
    {
        from: "<myemailingoogle@gmail.com>",//my email
        to: parametros.useremail,//user email
        subject: 'Welcome '+parametros.useremail+'.',
        html:   '<br><b>Hello</b><br>'+ // html 

    };
    var smtpTransport = nodemailer.createTransport("SMTP",
    {
        service: "Gmail",
        secureConnection: true,
        auth:
        {
            user: "myemailingoogle@gmail.com",
            pass: "7ftera359c28a",
        },
    });
    smtpTransport.sendMail(mailOptions, function(err, response)
    {
        if(err)
        {
            smtpTransport.close();
            console.warn(err);
            return callback(err,null);
        }else{
            smtpTransport.close();
            return callback(null,response);
        }
    });
}

电子邮件发送正常,但每天都有限制。

所以我需要使用电子邮件服务器无限制地发送我的电子邮件.... tnx

编辑2

我使用apt-get

安装mailutils

现在我尝试

mail mymailin@gmail.com
Cc: // press enter
Subject: Welcome

Hello Mail.

// Ctrl+D

这会将电子邮件发送到我的邮箱,并在垃圾邮件中收到从root@mail.mydomain.com发送的电子邮件

所以,postfix工作正常,但我仍然无法使用emailjs或nodemailer发送电子邮件......

当我尝试使用电子邮件或nodemailer发送电子邮件时,我得到了这个

smtp: '535 5.7.8 Error: authentication failed: generic failure\n'

我在谷歌搜索该错误并且是和auth登录错误,我无法登录系统。 所以我试试telnet

telnet localhost 25
ehlo localhost
250-mydomain.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
mail to: myuserin@gmail.com
501 5.5.4 Syntax: MAIL FROM:<address>
mail from: info@mydomain.com
250 2.1.0 Ok
rcpt to: myuserin@gmail.com
451 4.3.0 <myuserin@gmail.com>: Temporary lookup failure
data
554 5.5.1 Error: no valid recipients
AUTH LOGIN
503 5.5.1 Error: MAIL transaction in progress

我再试一次

220 mydomain.com ESMTP Postfix (Ubuntu)
ehlo localhost
250-mydomain.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
AUTH LOGIN
334 VXNlcm5hbWU6
UbuntuUser
535 5.7.8 Error: authentication failed: another step is needed in authentication

再次使用root

220 mydomain.com ESMTP Postfix (Ubuntu)
ehlo localhost
250-mydomain.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-AUTH PLAIN LOGIN
250-AUTH=PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
auth login
334 VXNlcm5hbWU6
root
334 UGFzc3dvcmQ6
rootPassword
535 5.7.8 Error: authentication failed: another step is needed in authentication

这是日志文件

Jun 20 15:50:16 myname postfix/smtpd[12528]: warning: localhost[127.0.0.1]:             SASL login authentication failed: another step is needed in authentication

就是这个......这个问题在哪里?

2 个答案:

答案 0 :(得分:7)

我也必须这样做,但我在Azure虚拟Ubuntu Linux服务器上运行我的节点服务器。我想如果你正在运行Ubuntu,那么从你的家庭服务器可以使用相同的步骤。这是我发现的工作:

我使用本指南安装了postfix电子邮件服务器:

https://help.ubuntu.com/community/Postfix

起初看起来很多工作,但它很容易为我工作。

我发现从node.js发送传出电子邮件的最简单方法是使用emailjs:

http://github.com/eleith/emailjs.git

我发现发送的电子邮件最终被标记为垃圾邮件。为了避免这种情况,您可以访问管理域名的任何地方(我使用enom),然后配置SPF记录以帮助您的外发电子邮件看起来合法。

这是我的node.js模块,用于使用emailjs发送电子邮件:

    var email   = require("emailjs");

    postfixSend = function postfixSend(emailInfo, callback) {

        var server  = email.server.connect({
            user:    "yourEmailAccountUser", 
            password: "yourPassword", 
            host:    "localhost", 
            ssl:     false
        });

        server.send({
            text:    emailInfo.msg, 
            from:    emailInfo.from, 
            to:      emailInfo.to,
            subject: emailInfo.subject
            }, function(err, message) {
                callback(err);
        });

    }

    exports.postfixSend = postfixSend;

答案 1 :(得分:1)

我解决了这个问题......

article using postfix

我在sasldb2中添加了一些用户,而且emailjs工作得非常好。

首先使用Svbaker tutorial安装后缀,但是,

编辑/etc/postfix/sasl/smtpd.conf而不是添加以下行:

pwcheck_method: auxprop
auxprop_plugin: sasldb 
mech_list: PLAIN LOGIN

thnx所有的帮助,特别是@Svbaker,为您提供解决此问题的方法。