是否有一个 nodejs 等效于 PHP 的 mail() 函数


Is there a nodejs equivalent to PHP's mail() function

我来自PHP世界,我习惯于偶尔使用mail()发送快速诊断电子邮件。在 NodeJS 的标准库中是否有一个模块或方法大致相当于这个?

当然:

const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({sendmail: true}, {
  from: 'no-reply@your-domain.com',
  to: 'your@mail.com',
  subject: 'test',
});
transporter.sendMail({text: 'hello'});

另请参阅在 docker 容器内配置 sendmail

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

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

完整使用看起来像这样(顶部位只是设置 - 所以你只需要为每个应用程序做一次):

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
});