在Node.js中发送电子邮件?

Nyc*_*Sci 277 email-integration node.js

我最近开始编程我的第一个node.js. 但是,我发现我无法创建直接发送到我的电子邮件的联系人表单,因为我找不到任何能够发送电子邮件的节点的模块.

有谁知道node.js电子邮件库或示例联系表单脚本?

小智 159

Nodemailer基本上是一个模块,使您能够在Node.js中编程时轻松发送电子邮件.有一些关于如何在http://www.nodemailer.com/上使用Nodemailer模块的很好的例子.有关如何安装和使用Nodemailer基本功能的完整说明,请参见此链接.

我个人在使用npm安装Nodemailer时遇到了麻烦,所以我刚刚下载了源代码.有关npm安装和下载源的说明.

这是一个非常简单的模块,我建议任何想要使用Node.js发送电子邮件的人.祝好运!

  • nodemailer最初工作! (2认同)
  • 这个答案应该包括一个例子 (2认同)

小智 140

node-email-templates是一个更好的选择:https: //github.com/niftylettuce/node-email-templates

它也支持Windows

  • 那是因为它的http://www.nodemailer.com/现在我在我的项目中使用它,工作正常,nodejitsu没有问题通过gmail的smtp服务器发送邮件. (7认同)
  • https://github.com/marak/node_mailer已弃用.应该使用这个https://github.com/andris9/Nodemailer (4认同)
  • 过去两天我花了几个小时试图让"node-email-templates"启动并运行.我想将它用作我初始化然后用于发送的对象.无法让它发挥作用.放弃. (2认同)

sil*_*vio 64

看看emailjs

在浪费了大量时间尝试使用大型附件进行nodemailer工作之后,发现了emailjs并且从此开心.

它支持使用普通的File对象发送文件,而不是像nodemailer所要求的那样使用巨大的Buffers.意味着你可以将它链接到fe,强大,以便将附件从html表单传递给邮件程序.它还支持排队..

总而言之,不知道为什么nodejitsu ppl选择了nodemailer来建立他们的版本,emailjs更加先进.

  • 更新到以前的评论,原作者也修复了这个bug和其他一些,所以不再需要fork.最新版本应该在[https://github.com/eleith/emailjs](https://github.com/eleith/emailjs)上 (18认同)
  • 经过一段时间的长时间使用,不得不分叉来解决问题:除非在邮件发送过程中,事情不会处理套接字事件,因此在闲置一段时间后停止,因为服务器会关闭连接,但那里没有什么可以处理事件并重置内部变量.你可以在这里找到分叉版本:[https://github.com/silvioster/emailjs](https://github.com/silvioster/emailjs).这个版本已经运行了很长一段时间,没有档位. (3认同)

Vic*_*cky 51

使用nodemailer模块完成代码以发送电子邮件

var mailer = require("nodemailer");

// Use Smtp Protocol to send Email
var smtpTransport = mailer.createTransport("SMTP",{
    service: "Gmail",
    auth: {
        user: "gmail_id@gmail.com",
        pass: "gmail_password"
    }
});

var mail = {
    from: "Yashwant Chavan <from@gmail.com>",
    to: "to@gmail.com",
    subject: "Send Email Using Node.js",
    text: "Node.js New world for me",
    html: "<b>Node.js New world for me</b>"
}

smtpTransport.sendMail(mail, function(error, response){
    if(error){
        console.log(error);
    }else{
        console.log("Message sent: " + response.message);
    }

    smtpTransport.close();
});
Run Code Online (Sandbox Code Playgroud)

  • 我发现`Nodemailer`*方式*比`node-email-templates`更容易使用.也许是因为我只是想发送基本的纯文本/基本的html电子邮件,但我发现`node-email-templates`需要更多的设置,而`Nodemailer`在2分钟内启动并运行. (3认同)
  • 除了gmail?我们如何配置自己的smtp服务器? (3认同)
  • @user1063287 只需删除“SMTP”参数 (2认同)

Dea*_*her 25

@ JimBastard接受的答案似乎已过时,我看了一下,邮件库已经超过7个月未被触及,列出了几个错误,并且不再在npm注册.

nodemailer肯定看起来是最好的选择,但是在这个帖子的其他答案中提供的url都是404'ing.

nodemailer声称支持轻松插入gmail,hotmail等,并且还有非常漂亮的文档.


Rob*_*den 9

你总是可以使用AlphaMail(披露:我是其背后的开发者之一).

只需安装NPM:

npm install alphamail
Run Code Online (Sandbox Code Playgroud)

注册一个AlphaMail帐户.获取令牌,然后您可以开始使用AlphaMail服务发送.

var alphamail = require('alphamail');

var emailService = new alphamail.EmailService()
    .setServiceUrl('http://api.amail.io/v1/')
    .setApiToken('YOUR-ACCOUNT-API-TOKEN-HERE');

var person = {
    id: 1234,
    userName: "jdoe75",
    name: {
        first: "John",
        last: "Doe"
    },
    dateOfBirth: 1975
};

emailService.queue(new alphamail.EmailMessagePayload()
    .setProjectId(12345) // ID of your AlphaMail project (determines template, options, etc)
    .setSender(new alphamail.EmailContact("Sender Company Name", "from@example.com"))
    .setReceiver(new alphamail.EmailContact("John Doe", "to@example.org"))
    .setBodyObject(person) // Any serializable object
);
Run Code Online (Sandbox Code Playgroud)

在AlphaMail GUI(仪表板)中,您将能够使用您发送的数据编辑模板:

<html>
    <body>
        <b>Name:</b> <# payload.name.last " " payload.name.first #><br>
        <b>Date of Birth:</b> <# payload.dateOfBirth #><br>

        <# if (payload.id != null) { #>
            <a href="http://company.com/sign-up">Sign Up Free!</a>
        <# } else { #>
            <a href="http://company.com/login?username=<# urlencode(payload.userName) #>">Sign In</a>
        <# } #>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

模板是用Comlang编写的,它是一种专门为电子邮件设计的简单模板语言.

  • Alphamail现在正式死亡.非常不幸 - 我开始使用它正是因为这篇文章.祝愿开发人员在下一次冒险中一切顺利. (12认同)

小智 8

成熟,易于使用,并且具有许多功能,如果不简单:Nodemailer:https://github.com/andris9/nodemailer(注意正确的网址!)


小智 6

Nodemailer模块是在node.js中发送电子邮件的最简单方法.

试试这个示例示例表单:http://www.tutorialindustry.com/nodejs-mail-tutorial-using-nodemailer-module

其他信息:http://www.nodemailer.com/

  • http://www.tutorialindustry.com/nodejs-mail-tutorial-using-nodemailer-module不能正常工作! (3认同)