20 Responses so far.
Leave a Comment
平时在开发应用的时候,很多时候会用到邮件,来通知我们, 这里介绍 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>'
]
}
};
参数:
如何使用:
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>');
however this article presents nice understanding yet.
the good work!
part 2?
things to improve my web site!I assume its good enough to make
use of some of your concepts!!
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!!
the site is really good.
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.
website, that’s what this site is providing.
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!
Is going to be again continuously in order to check out new posts