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

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


当前回答

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

其他回答

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

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

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

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

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

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

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

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

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

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

你像这样实例化它。

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

要发送电子邮件,你可以使用Mandrill,它是免费的,很棒。只需要设置你的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回购有相当广泛的文档。

查看emailjs

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

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

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