使用 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 Responses so far.

  1. quest bars says:
    Asking questions are really pleasant thing if you are not understanding anything completely,
    however this article presents nice understanding yet.
  2. quest bars says:
    Hi, I read your blog daily. Your writing style is witty, keep up
    the good work!
  3. Highly energetic blog, I loved that a lot. Will there be a
    part 2?
  4. This info is priceless. Where can I find out more?
  5. 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!!
  6. A fascinating discussion is definitely worth comment.
    I believe that you ought to publish more on this topic, it may not be a
    taboo matter but generally people do not discuss these issues.
    To the next! Cheers!!
  7. Way cool! Some extremely valid points! I appreciate you penning this article and also the rest of
    the site is really good.
  8. 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.
  9. Quality articles or reviews is the main to attract the visitors to go to see the
    website, that’s what this site is providing.
  10. 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!
  11. Your means of describing the whole thing in this piece of writing is genuinely good, every one be able to simply be aware of it, Thanks a lot.
  12. Keep on working, great job!
  13. 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
  14. I am always thought about this, regards for putting up.
  15. muscle girl says:
    Very informative blog post. Great.
  16. 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.
  17. nonton semi says:
    Really enjoyed this blog post.Thanks Again. Fantastic.
  18. I loved your blog article.Really thank you! Keep writing.
  19. Im thankful for the article.Really thank you! Will read on…
  20. I really enjoy the post.Much thanks again. Want more.

LEAVE A COMMENT