是否有一个相当于PHP的mail()函数的nodejs [英] Is there a nodejs equivalent to PHP's mail() function

查看:72
本文介绍了是否有一个相当于PHP的mail()函数的nodejs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自PHP领域,习惯于使用mail()偶尔发送快速诊断电子邮件。
NodeJS的标准库中是否存在与之大致等效的模块或方法?

I come from the world of PHP and I'm accustomed to using mail() to send quick diagnostic emails on occasion. Is there a module or method in the standar library of NodeJS that's roughly the equivalent of this?

推荐答案

Nodemailer是一种流行,稳定且灵活的解决方案:

Nodemailer is a popular, stable, and flexible solution:

  • http://www.nodemailer.com/
  • https://github.com/andris9/Nodemailer

完整用法看起来像这样(最高位只是设置-因此您每个应用程序只需要这样做一次):

Full usage looks something like this (the top bit is just setup - so you would only have to do that once per app):

var nodemailer = require("nodemailer");

// create reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer.createTransport("SMTP",{
    service: "Gmail",
    auth: {
        user: "gmail.user@gmail.com",
        pass: "userpass"
    }
});

// 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
smtpTransport.sendMail(mailOptions, function(error, response){
    if(error){
        console.log(error);
    }else{
        console.log("Message sent: " + response.message);
    }

    // if you don't want to use this transport object anymore, uncomment following line
    //smtpTransport.close(); // shut down the connection pool, no more messages
});

这篇关于是否有一个相当于PHP的mail()函数的nodejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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