9

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

4

2 回答 2

14

当然:

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

于 2017-08-27T18:27:56.373 回答
9

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 ✔&lt;/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
});
于 2013-02-03T23:10:59.963 回答