用Node.js发送电子邮件?

我最近开始编写我的第一个node.js。然而,我发现我无法创建一个联系我的形式,直接发送到我的电子邮件,因为我找不到任何模块从节点,能够发送电子邮件。

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

271358 次浏览

npm有一些包,但还没有达到1.0。npm list mail的最佳选择:

email@0.2.2
mail@0.1.1
mailer@0.3.0

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

它也支持Windows

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

我个人在使用npm安装Nodemailer时遇到了麻烦,所以我只是下载了源代码。这里有npm安装和下载源代码的说明。

这是一个非常简单的模块,我将它推荐给任何想要使用Node.js发送电子邮件的人。好运!

查看emailjs

在浪费了大量时间试图使nodemailer工作与大附件,发现emailjs和快乐的从那时起。

它支持通过使用普通的File对象来发送文件,而不是像nodemailer那样需要巨大的buffer。这意味着您可以将其链接到,例如,将附件从html表单传递到邮件发送器。它还支持排队..

总而言之,不知道nodejitsu ppl为什么选择nodemailer作为他们的版本的基础,emailjs只是更高级。

@JimBastard接受的答案似乎是过时的,我看了一下,那个mailer库已经超过7个月没有碰过了,列出了几个bug,并且不再在npm中注册。

nodemailer当然看起来是最好的选择,但是在这个线程的其他答案中提供的url都是404'ing。

Nodemailer声称支持简单的插件到gmail, hotmail等,也有非常漂亮的文档。

成熟,使用简单,如果简单还不够的话,有很多功能: Nodemailer: https://github.com/andris9/nodemailer(注意正确的url!)

你肯定想要使用https://github.com/niftylettuce/node-email-templates,因为它支持nodemailer/postmarkapp,并且内置了漂亮的异步电子邮件模板支持。

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

使用NPM安装即可:

npm install alphamail

注册一个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
);

在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>

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

运动是一个在Node中发送电子邮件的综合解决方案,它带有一个非常简单的API。

你像这样实例化它。

var client = require('campaign')({
from: 'you@gmail.com'
});

要发送电子邮件,你可以使用山魈,这是免费的,很棒。只需要设置你的API键,像这样:

process.env.MANDRILL_APIKEY = '<your api key>';

(如果您想使用其他提供商发送电子邮件,请检查文档)

然后,当你想发送电子邮件时,你可以这样做:

client.sendString('<p>\{\{something}}</p>', {
to: ['someone@gmail.com', 'someone.else@gmail.com'],
subject: 'Some Subject',
preview': 'The first line',
something: 'this is what replaces that thing in the template'
}, done);

GitHub回购有相当广泛的文档

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

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

附加信息:http://www.nodemailer.com/

使用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();
});