使用 nodejs 发送邮件 Nodemailer

平时在开发应用的时候,很多时候会用到邮件,来通知我们, 这里介绍 nodejs 如何发送邮件,比较简单。

使用 Nodemailer 库来发送邮件。

https://github.com/andris9/Nodemailer#migration-guide

This is a complete example to send an e-mail with plaintext and HTML body

var nodemailer = require('nodemailer');

// create reusable transporter object using SMTP transport
var transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
        user: 'gmail.user@gmail.com',
        pass: 'userpass'
    }
});

// NB! No need to recreate the transporter object. You can use
// the same transporter object for all e-mails

// 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
transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }
    console.log('Message sent: ' + info.response);

});

See nodemailer-smtp-transport for SMTP configuration options and nodemailer-wellknown for preconfigured service names (example uses ‘gmail’).

When using default SMTP transport, then you do not need to define transport type explicitly (even though you can), just provide the SMTP options and that’s it. For anything else, see the docs of the particular transport mechanism.

 

安装:

npm install nodemailer --save

增加配置信息 config.js:

module.exports = {
    mail: {
        from: {
            name: 'App name',
            service: 'Gmail',
            auth: {
                user: 'gmail.name@gmail.com',
                pass: 'gmail.password'
            }
        },
        to: [
            'Zhixin Wen <wenzhixin2010@gmail.com>'
        ]
    }
};

参数:

  • from:配置发送邮件信息
  • to:数组,配置发送给谁
  • name:显示的名称
  • service:SMTP 名称,这里用 Gmail
  • auth:邮箱的用户名和密码

如何使用:

var nodemailer = require('nodemailer'),
    config = require('./config'),
    smtpTransport = nodemailer.createTransport('SMTP', config.mail.from);

定义并且根据配置文件生成 smtpTransport。

发送邮件函数:

/**
 * @param {String} subject:发送的主题
 * @param {String} html:发送的 html 内容
 */
function sendMail(subject, html) {
    var mailOptions = {
        from: [config.mail.from.name, config.mail.from.auth.user].join(' '),
        to: config.mail.to.join(','),
        subject: subject,
        html: html
    };

    smtpTransport.sendMail(mailOptions, function(error, response){
        if (error) {
            console.log(error);
        } else {
            console.log('Message sent: ' + response.message);
        }
        smtpTransport.close();
    });
};

发邮件例子:

sendMail('测试发邮件', '<p>Hello world!</p>');

 

20 thoughts on “使用 nodejs 发送邮件 Nodemailer

  1. Hello, i feel that i saw you visited my site so i got here to go back the favor?.I’m attempting to in finding
    things to improve my web site!I assume its good enough to make
    use of some of your concepts!!

  2. Hello, i read your blog from time to time and i own a similar one and i was just wondering if you get a lot of
    spam comments? If so how do you prevent it, any plugin or anything
    you can recommend? I get so much lately it’s driving me crazy so any help
    is very much appreciated.

  3. you’re actually a good webmaster. The website loading speed is
    amazing. It kind of feels that you’re doing any distinctive
    trick. Also, The contents are masterwork. you’ve performed a great activity in this topic!

  4. I just couldn’t leave your website before suggesting that I really loved the usual info an individual supply to your visitors?
    Is going to be again continuously in order to check out new posts

  5. I have been browsing on-line greater than three hours as of late, yet I never found any interesting article like yours. It¦s lovely value enough for me. In my view, if all site owners and bloggers made excellent content material as you probably did, the web might be much more helpful than ever before.

Leave a Reply

Your email address will not be published. Required fields are marked *