我最近开始编写我的第一个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();
});

其他回答

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

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

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回购有相当广泛的文档。

使用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发送电子邮件的人。好运!