如何创建自定义的smtp服务器以在Node.js中发送通知电子邮件? [英] How can I create a custom smtp server to send out notification emails in Nodejs?

查看:460
本文介绍了如何创建自定义的smtp服务器以在Node.js中发送通知电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是从我的应用程序向任何电子邮件ID发送通知电子邮件,例如:gmail地址。我经历了一些模块,例如 smtp服务器 smtp-connection emailjs

My requirement is to send a notification email from my application to any email id , eg: a gmail address. I went through some modules like the smtp-server ,smtp-connection and emailjs

这是我到目前为止所得到的。

This is what I have got till now.

var SMTPServer = require('smtp-server').SMTPServer

var server = new SMTPServer({
  name: 'testDomain.com',
  authOptional: true,
  onAuth: function (auth, session, callback) {
    callback(null, {user: 'sample-user'}) 
  }
})
server.on('error', function (err) {
  console.log('Error %s', err.message)
})

var port = 1234

server.listen(port, function () {
  console.log('SERVER: Listening on port: ' + port)
  var opts = {
    host: '127.0.0.1',
    port: port,
    username: 'testUser',
    password: 'testUser123',
    to: 'someUser@gmail.com'
  }
  sendEmail(opts,function (err, message) {
    server.close()
  })
})

其中sendEmail是使用emailjs的函数。

where sendEmail is a function using emailjs.

 function sendEmail(opts,callback) {
  var server = email.server.connect({
    user: opts.username || '',
    password: opts.password || '',
    host: opts.host,
    ssl: false
  })

  server.send({
    text: 'i hope this works',
    from: 'you <'+opts.username+'@testDomain.com>',
    to: ' <'+opts.to+'>',
    subject: 'testing emailjs'
  }, function (err, message) {
     console.log(err || message);
     callback(err, message)
    })
}

但客户端似乎无法连接到服务器。

But it seems that the client is not able to connect to the server. It is hanging.

我最初尝试这样的smtp连接:

I tried smtp-connection like this initially:

var connection = new SMTPConnection({
    port: port,
    host: '127.0.0.1',
    ignoreTLS: true
  })

  connection.connect(function () {
    var envelope = {
       from: opts.username+'@testDomain.com',
       to: opts.to
    }
    var message = "Hello!!!"
    connection.send(envelope, message, function(err,message){
      callback(err,message)
      connection.quit()

    })

这似乎可行,但给出的输出结果

This seems to work but gives this output

response: '250 OK: message queued'

smtp连接文档说,它仅将邮件排队,不会将其传递给收件人。

the smtp-connection documentation says it only queues the messages doesnt deliver it to the recipient.

我如何达到我的要求?从自定义邮件服务器发送通知,因为我要避免添加用户c代码中纯文本形式的电子邮件帐户的凭据。我正在寻找一个简单的邮件服务器,当需要发送通知然后将其关闭时可以将其旋转。

How can I achieve my requirement? I am attempting to send the notification from a custom mail server because I want to avoid adding the user credentials of an email account in the code in plaintext. I am looking for a simple mailserver which can be spun up when the notification needs to be sent and then shut down.

我完全偏离了轨道,不了解邮件服务器的工作方式??请提供一些反馈和最佳方法来解决此问题。

Am I completely offtrack, not understanding how mail servers work?? Please give some feedback and a best approach to solve this.

推荐答案

我的看法是我的看法,但我认为最好单独寄一封邮件服务器。
就像 nodemailer 中的示例一样:

Just my opinion but I think its better to take a separate mail server. like the example from nodemailer:

var nodemailer = require('nodemailer');

// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport('smtps://user%40gmail.com:pass@smtp.gmail.com');

// setup e-mail data with unicode symbols
var mailOptions = {
    from: '"Fred Foo ?" <foo@blurdybloop.com>', // sender address
    to: 'bar@blurdybloop.com, baz@blurdybloop.com', // list of receivers
    subject: 'Hello ✔', // Subject line
    text: 'Hello world ?', // plaintext body
    html: '<b>Hello world ?</b>' // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }
    console.log('Message sent: ' + info.response);
});

出于安全考虑:


  • 您可以使用单独的文件来存储用户名/密码。

  • You can use separate file for storing the username / password.

使用可以使用基于令牌的身份验证。因此,您无需保存密码。 OAuth就是一个例子。使用令牌进行身份验证,而不是密码。您从邮件服务器提供商(例如gmail)获得此令牌。
使用oauth和nodemailer的示例,您可以在此处

Use can use a Token based authentication. So you don't need to save the password. An example of this is OAuth. Instead of the password you authenticate with a token. This token u get from the mailserver provider (like gmail). An example use of oauth and nodemailer you can find here.

这篇关于如何创建自定义的smtp服务器以在Node.js中发送通知电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆